Subversion Repositories Sites.tela-botanica.org

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 david 1
<?php
2
 
3
// --------------------------------------------------------------------------------
4
// PhpConcept Library - Zip Module 2.3
5
// --------------------------------------------------------------------------------
6
// License GNU/LGPL - Vincent Blavet - November 2004
7
// http://www.phpconcept.net
8
// --------------------------------------------------------------------------------
9
//
10
// Presentation :
11
//   PclZip is a PHP library that manage ZIP archives.
12
//   So far tests show that archives generated by PclZip are readable by
13
//   WinZip application and other tools.
14
//
15
// Description :
16
//   See readme.txt and http://www.phpconcept.net
17
//
18
// Warning :
19
//   This library and the associated files are non commercial, non professional
20
//   work.
21
//   It should not have unexpected results. However if any damage is caused by
22
//   this software the author can not be responsible.
23
//   The use of this software is at the risk of the user.
24
//
25
// --------------------------------------------------------------------------------
26
// $Id: pclzip.lib.php,v 1.7 2005/04/08 08:41:29 fil Exp $
27
// --------------------------------------------------------------------------------
28
 
29
  // ----- Constants
30
  define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
31
 
32
  // ----- File list separator
33
  // In version 1.x of PclZip, the separator for file list is a space
34
  // (which is not a very smart choice, specifically for windows paths !).
35
  // A better separator should be a comma (,). This constant gives you the
36
  // abilty to change that.
37
  // However notice that changing this value, may have impact on existing
38
  // scripts, using space separated filenames.
39
  // Recommanded values for compatibility with older versions :
40
  //define( 'PCLZIP_SEPARATOR', ' ' );
41
  // Recommanded values for smart separation of filenames.
42
  define( 'PCLZIP_SEPARATOR', ',' );
43
 
44
  // ----- Error configuration
45
  // 0 : PclZip Class integrated error handling
46
  // 1 : PclError external library error handling. By enabling this
47
  //     you must ensure that you have included PclError library.
48
  // [2,...] : reserved for futur use
49
  define( 'PCLZIP_ERROR_EXTERNAL', 0 );
50
 
51
  // ----- Optional static temporary directory
52
  //       By default temporary files are generated in the script current
53
  //       path.
54
  //       If defined :
55
  //       - MUST BE terminated by a '/'.
56
  //       - MUST be a valid, already created directory
57
  //       Samples :
58
  // define( 'PCLZIP_TEMPORARY_DIR', '/temp/' );
59
  // define( 'PCLZIP_TEMPORARY_DIR', 'C:/Temp/' );
60
  define( 'PCLZIP_TEMPORARY_DIR', '' );
61
 
62
// --------------------------------------------------------------------------------
63
// ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
64
// --------------------------------------------------------------------------------
65
 
66
  // ----- Global variables
67
  $g_pclzip_version = "2.3";
68
 
69
  // ----- Error codes
70
  //   -1 : Unable to open file in binary write mode
71
  //   -2 : Unable to open file in binary read mode
72
  //   -3 : Invalid parameters
73
  //   -4 : File does not exist
74
  //   -5 : Filename is too long (max. 255)
75
  //   -6 : Not a valid zip file
76
  //   -7 : Invalid extracted file size
77
  //   -8 : Unable to create directory
78
  //   -9 : Invalid archive extension
79
  //  -10 : Invalid archive format
80
  //  -11 : Unable to delete file (unlink)
81
  //  -12 : Unable to rename file (rename)
82
  //  -13 : Invalid header checksum
83
  //  -14 : Invalid archive size
84
  define( 'PCLZIP_ERR_USER_ABORTED', 2 );
85
  define( 'PCLZIP_ERR_NO_ERROR', 0 );
86
  define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
87
  define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
88
  define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
89
  define( 'PCLZIP_ERR_MISSING_FILE', -4 );
90
  define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
91
  define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
92
  define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
93
  define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
94
  define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
95
  define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
96
  define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
97
  define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
98
  define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
99
  define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
100
  define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
101
  define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
102
  define( 'PCLZIP_ERR_ALREADY_A_DIRECTORY', -17 );
103
  define( 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18 );
104
  define( 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19 );
105
 
106
  // ----- Options values
107
  define( 'PCLZIP_OPT_PATH', 77001 );
108
  define( 'PCLZIP_OPT_ADD_PATH', 77002 );
109
  define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
110
  define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
111
  define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
112
  define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
113
  define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
114
  define( 'PCLZIP_OPT_BY_NAME', 77008 );
115
  define( 'PCLZIP_OPT_BY_INDEX', 77009 );
116
  define( 'PCLZIP_OPT_BY_EREG', 77010 );
117
  define( 'PCLZIP_OPT_BY_PREG', 77011 );
118
  define( 'PCLZIP_OPT_COMMENT', 77012 );
119
  define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
120
  define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
121
  define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
122
  define( 'PCLZIP_OPT_REPLACE_NEWER', 77016 );
123
  define( 'PCLZIP_OPT_STOP_ON_ERROR', 77017 );
124
  // Having big trouble with crypt. Need to multiply 2 long int
125
  // which is not correctly supported by PHP ...
126
  //define( 'PCLZIP_OPT_CRYPT', 77018 );
127
 
128
  // ----- Call backs values
129
  define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
130
  define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
131
  define( 'PCLZIP_CB_PRE_ADD', 78003 );
132
  define( 'PCLZIP_CB_POST_ADD', 78004 );
133
  /* For futur use
134
  define( 'PCLZIP_CB_PRE_LIST', 78005 );
135
  define( 'PCLZIP_CB_POST_LIST', 78006 );
136
  define( 'PCLZIP_CB_PRE_DELETE', 78007 );
137
  define( 'PCLZIP_CB_POST_DELETE', 78008 );
138
  */
139
 
140
  // --------------------------------------------------------------------------------
141
  // Class : PclZip
142
  // Description :
143
  //   PclZip is the class that represent a Zip archive.
144
  //   The public methods allow the manipulation of the archive.
145
  // Attributes :
146
  //   Attributes must not be accessed directly.
147
  // Methods :
148
  //   PclZip() : Object creator
149
  //   create() : Creates the Zip archive
150
  //   listContent() : List the content of the Zip archive
151
  //   extract() : Extract the content of the archive
152
  //   properties() : List the properties of the archive
153
  // --------------------------------------------------------------------------------
154
   class PclZip
155
  {
156
    // ----- Filename of the zip file
157
    var $zipname = '';
158
 
159
    // ----- File descriptor of the zip file
160
    var $zip_fd = 0;
161
 
162
    // ----- Internal error handling
163
    var $error_code = 1;
164
    var $error_string = '';
165
 
166
  // --------------------------------------------------------------------------------
167
  // Function : PclZip()
168
  // Description :
169
  //   Creates a PclZip object and set the name of the associated Zip archive
170
  //   filename.
171
  //   Note that no real action is taken, if the archive does not exist it is not
172
  //   created. Use create() for that.
173
  // --------------------------------------------------------------------------------
174
  function PclZip($p_zipname)
175
  {
176
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::PclZip', "zipname=$p_zipname");
177
 
178
    // ----- Tests the zlib
179
    if (!function_exists('gzopen'))
180
    {
181
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 1, "zlib extension seems to be missing");
182
      die('Abort '.basename(__FILE__).' : Missing zlib extensions');
183
    }
184
 
185
    // ----- Set the attributes
186
    $this->zipname = $p_zipname;
187
    $this->zip_fd = 0;
188
 
189
    // ----- Return
190
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 1);
191
    return;
192
  }
193
  // --------------------------------------------------------------------------------
194
 
195
  // --------------------------------------------------------------------------------
196
  // Function :
197
  //   create($p_filelist, $p_add_dir="", $p_remove_dir="")
198
  //   create($p_filelist, $p_option, $p_option_value, ...)
199
  // Description :
200
  //   This method supports two different synopsis. The first one is historical.
201
  //   This method creates a Zip Archive. The Zip file is created in the
202
  //   filesystem. The files and directories indicated in $p_filelist
203
  //   are added in the archive. See the parameters description for the
204
  //   supported format of $p_filelist.
205
  //   When a directory is in the list, the directory and its content is added
206
  //   in the archive.
207
  //   In this synopsis, the function takes an optional variable list of
208
  //   options. See bellow the supported options.
209
  // Parameters :
210
  //   $p_filelist : An array containing file or directory names, or
211
  //                 a string containing one filename or one directory name, or
212
  //                 a string containing a list of filenames and/or directory
213
  //                 names separated by spaces.
214
  //   $p_add_dir : A path to add before the real path of the archived file,
215
  //                in order to have it memorized in the archive.
216
  //   $p_remove_dir : A path to remove from the real path of the file to archive,
217
  //                   in order to have a shorter path memorized in the archive.
218
  //                   When $p_add_dir and $p_remove_dir are set, $p_remove_dir
219
  //                   is removed first, before $p_add_dir is added.
220
  // Options :
221
  //   PCLZIP_OPT_ADD_PATH :
222
  //   PCLZIP_OPT_REMOVE_PATH :
223
  //   PCLZIP_OPT_REMOVE_ALL_PATH :
224
  //   PCLZIP_OPT_COMMENT :
225
  //   PCLZIP_CB_PRE_ADD :
226
  //   PCLZIP_CB_POST_ADD :
227
  // Return Values :
228
  //   0 on failure,
229
  //   The list of the added files, with a status of the add action.
230
  //   (see PclZip::listContent() for list entry format)
231
  // --------------------------------------------------------------------------------
232
//  function create($p_filelist, $p_add_dir="", $p_remove_dir="")
233
  function create($p_filelist /*, options */)
234
  {
235
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::create', "filelist='$p_filelist', ...");
236
    $v_result=1;
237
 
238
    // ----- Reset the error handler
239
    $this->privErrorReset();
240
 
241
    // ----- Set default values
242
    $v_options = array();
243
    $v_add_path = "";
244
    $v_remove_path = "";
245
    $v_remove_all_path = false;
246
    $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
247
 
248
    // ----- Look for variable options arguments
249
    $v_size = func_num_args();
250
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
251
 
252
    // ----- Look for arguments
253
    if ($v_size > 1) {
254
      // ----- Get the arguments
255
      $v_arg_list = &func_get_args();
256
 
257
      // ----- Remove form the options list the first argument
258
      array_shift($v_arg_list);
259
      $v_size--;
260
 
261
      // ----- Look for first arg
262
      if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
263
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
264
 
265
        // ----- Parse the options
266
        $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
267
                                            array (PCLZIP_OPT_REMOVE_PATH => 'optional',
268
                                                   PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
269
                                                   PCLZIP_OPT_ADD_PATH => 'optional',
270
                                                   PCLZIP_CB_PRE_ADD => 'optional',
271
                                                   PCLZIP_CB_POST_ADD => 'optional',
272
                                                   PCLZIP_OPT_NO_COMPRESSION => 'optional',
273
                                                   PCLZIP_OPT_COMMENT => 'optional'
274
                                                   //, PCLZIP_OPT_CRYPT => 'optional'
275
												   ));
276
        if ($v_result != 1) {
277
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
278
          return 0;
279
        }
280
 
281
        // ----- Set the arguments
282
        if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
283
          $v_add_path = $v_options[PCLZIP_OPT_ADD_PATH];
284
        }
285
        if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
286
          $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
287
        }
288
        if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
289
          $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
290
        }
291
      }
292
 
293
      // ----- Look for 2 args
294
      // Here we need to support the first historic synopsis of the
295
      // method.
296
      else {
297
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
298
 
299
        // ----- Get the first argument
300
        $v_add_path = $v_arg_list[0];
301
 
302
        // ----- Look for the optional second argument
303
        if ($v_size == 2) {
304
          $v_remove_path = $v_arg_list[1];
305
        }
306
        else if ($v_size > 2) {
307
          // ----- Error log
308
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
309
		                       "Invalid number / type of arguments");
310
 
311
          // ----- Return
312
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
313
          return 0;
314
        }
315
      }
316
    }
317
 
318
    // ----- Trace
319
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "add_path='$v_add_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_all_path?'true':'false')."'");
320
 
321
    // ----- Look if the $p_filelist is really an array
322
    $p_result_list = array();
323
    if (is_array($p_filelist))
324
    {
325
      // ----- Call the create fct
326
      $v_result = $this->privCreate($p_filelist, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
327
    }
328
 
329
    // ----- Look if the $p_filelist is a string
330
    else if (is_string($p_filelist))
331
    {
332
      // ----- Create a list with the elements from the string
333
      $v_list = explode(PCLZIP_SEPARATOR, $p_filelist);
334
 
335
      // ----- Call the create fct
336
      $v_result = $this->privCreate($v_list, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
337
    }
338
 
339
    // ----- Invalid variable
340
    else
341
    {
342
      // ----- Error log
343
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
344
      $v_result = PCLZIP_ERR_INVALID_PARAMETER;
345
    }
346
 
347
    if ($v_result != 1)
348
    {
349
      // ----- Return
350
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
351
      return 0;
352
    }
353
 
354
    // ----- Return
355
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
356
    return $p_result_list;
357
  }
358
  // --------------------------------------------------------------------------------
359
 
360
  // --------------------------------------------------------------------------------
361
  // Function :
362
  //   add($p_filelist, $p_add_dir="", $p_remove_dir="")
363
  //   add($p_filelist, $p_option, $p_option_value, ...)
364
  // Description :
365
  //   This method supports two synopsis. The first one is historical.
366
  //   This methods add the list of files in an existing archive.
367
  //   If a file with the same name already exists, it is added at the end of the
368
  //   archive, the first one is still present.
369
  //   If the archive does not exist, it is created.
370
  // Parameters :
371
  //   $p_filelist : An array containing file or directory names, or
372
  //                 a string containing one filename or one directory name, or
373
  //                 a string containing a list of filenames and/or directory
374
  //                 names separated by spaces.
375
  //   $p_add_dir : A path to add before the real path of the archived file,
376
  //                in order to have it memorized in the archive.
377
  //   $p_remove_dir : A path to remove from the real path of the file to archive,
378
  //                   in order to have a shorter path memorized in the archive.
379
  //                   When $p_add_dir and $p_remove_dir are set, $p_remove_dir
380
  //                   is removed first, before $p_add_dir is added.
381
  // Options :
382
  //   PCLZIP_OPT_ADD_PATH :
383
  //   PCLZIP_OPT_REMOVE_PATH :
384
  //   PCLZIP_OPT_REMOVE_ALL_PATH :
385
  //   PCLZIP_OPT_COMMENT :
386
  //   PCLZIP_OPT_ADD_COMMENT :
387
  //   PCLZIP_OPT_PREPEND_COMMENT :
388
  //   PCLZIP_CB_PRE_ADD :
389
  //   PCLZIP_CB_POST_ADD :
390
  // Return Values :
391
  //   0 on failure,
392
  //   The list of the added files, with a status of the add action.
393
  //   (see PclZip::listContent() for list entry format)
394
  // --------------------------------------------------------------------------------
395
//  function add($p_filelist, $p_add_dir="", $p_remove_dir="")
396
  function add($p_filelist /* options */)
397
  {
398
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::add', "filelist='$p_filelist', ...");
399
    $v_result=1;
400
 
401
    // ----- Reset the error handler
402
    $this->privErrorReset();
403
 
404
    // ----- Set default values
405
    $v_options = array();
406
    $v_add_path = "";
407
    $v_remove_path = "";
408
    $v_remove_all_path = false;
409
    $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
410
 
411
    // ----- Look for variable options arguments
412
    $v_size = func_num_args();
413
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
414
 
415
    // ----- Look for arguments
416
    if ($v_size > 1) {
417
      // ----- Get the arguments
418
      $v_arg_list = &func_get_args();
419
 
420
      // ----- Remove form the options list the first argument
421
      array_shift($v_arg_list);
422
      $v_size--;
423
 
424
      // ----- Look for first arg
425
      if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
426
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
427
 
428
        // ----- Parse the options
429
        $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
430
                                            array (PCLZIP_OPT_REMOVE_PATH => 'optional',
431
                                                   PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
432
                                                   PCLZIP_OPT_ADD_PATH => 'optional',
433
                                                   PCLZIP_CB_PRE_ADD => 'optional',
434
                                                   PCLZIP_CB_POST_ADD => 'optional',
435
                                                   PCLZIP_OPT_NO_COMPRESSION => 'optional',
436
                                                   PCLZIP_OPT_COMMENT => 'optional',
437
                                                   PCLZIP_OPT_ADD_COMMENT => 'optional',
438
                                                   PCLZIP_OPT_PREPEND_COMMENT => 'optional'
439
                                                   //, PCLZIP_OPT_CRYPT => 'optional'
440
												   ));
441
        if ($v_result != 1) {
442
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
443
          return 0;
444
        }
445
 
446
        // ----- Set the arguments
447
        if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
448
          $v_add_path = $v_options[PCLZIP_OPT_ADD_PATH];
449
        }
450
        if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
451
          $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
452
        }
453
        if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
454
          $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
455
        }
456
      }
457
 
458
      // ----- Look for 2 args
459
      // Here we need to support the first historic synopsis of the
460
      // method.
461
      else {
462
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
463
 
464
        // ----- Get the first argument
465
        $v_add_path = $v_arg_list[0];
466
 
467
        // ----- Look for the optional second argument
468
        if ($v_size == 2) {
469
          $v_remove_path = $v_arg_list[1];
470
        }
471
        else if ($v_size > 2) {
472
          // ----- Error log
473
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
474
 
475
          // ----- Return
476
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
477
          return 0;
478
        }
479
      }
480
    }
481
 
482
    // ----- Trace
483
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "add_path='$v_add_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_all_path?'true':'false')."'");
484
 
485
    // ----- Look if the $p_filelist is really an array
486
    $p_result_list = array();
487
    if (is_array($p_filelist))
488
    {
489
      // ----- Call the create fct
490
      $v_result = $this->privAdd($p_filelist, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
491
    }
492
 
493
    // ----- Look if the $p_filelist is a string
494
    else if (is_string($p_filelist))
495
    {
496
      // ----- Create a list with the elements from the string
497
      $v_list = explode(PCLZIP_SEPARATOR, $p_filelist);
498
 
499
      // ----- Call the create fct
500
      $v_result = $this->privAdd($v_list, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
501
    }
502
 
503
    // ----- Invalid variable
504
    else
505
    {
506
      // ----- Error log
507
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
508
      $v_result = PCLZIP_ERR_INVALID_PARAMETER;
509
    }
510
 
511
    if ($v_result != 1)
512
    {
513
      // ----- Return
514
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
515
      return 0;
516
    }
517
 
518
    // ----- Return
519
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
520
    return $p_result_list;
521
  }
522
  // --------------------------------------------------------------------------------
523
 
524
  // --------------------------------------------------------------------------------
525
  // Function : listContent()
526
  // Description :
527
  //   This public method, gives the list of the files and directories, with their
528
  //   properties.
529
  //   The properties of each entries in the list are (used also in other functions) :
530
  //     filename : Name of the file. For a create or add action it is the filename
531
  //                given by the user. For an extract function it is the filename
532
  //                of the extracted file.
533
  //     stored_filename : Name of the file / directory stored in the archive.
534
  //     size : Size of the stored file.
535
  //     compressed_size : Size of the file's data compressed in the archive
536
  //                       (without the headers overhead)
537
  //     mtime : Last known modification date of the file (UNIX timestamp)
538
  //     comment : Comment associated with the file
539
  //     folder : true | false
540
  //     index : index of the file in the archive
541
  //     status : status of the action (depending of the action) :
542
  //              Values are :
543
  //                ok : OK !
544
  //                filtered : the file / dir is not extracted (filtered by user)
545
  //                already_a_directory : the file can not be extracted because a
546
  //                                      directory with the same name already exists
547
  //                write_protected : the file can not be extracted because a file
548
  //                                  with the same name already exists and is
549
  //                                  write protected
550
  //                newer_exist : the file was not extracted because a newer file exists
551
  //                path_creation_fail : the file is not extracted because the folder
552
  //                                     does not exists and can not be created
553
  //                write_error : the file was not extracted because there was a
554
  //                              error while writing the file
555
  //                read_error : the file was not extracted because there was a error
556
  //                             while reading the file
557
  //                invalid_header : the file was not extracted because of an archive
558
  //                                 format error (bad file header)
559
  //   Note that each time a method can continue operating when there
560
  //   is an action error on a file, the error is only logged in the file status.
561
  // Return Values :
562
  //   0 on an unrecoverable failure,
563
  //   The list of the files in the archive.
564
  // --------------------------------------------------------------------------------
565
  function listContent()
566
  {
567
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::listContent', "");
568
    $v_result=1;
569
 
570
    // ----- Reset the error handler
571
    $this->privErrorReset();
572
 
573
    // ----- Check archive
574
    if (!$this->privCheckFormat()) {
575
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
576
      return(0);
577
    }
578
 
579
    // ----- Call the extracting fct
580
    $p_list = array();
581
    if (($v_result = $this->privList($p_list)) != 1)
582
    {
583
      unset($p_list);
584
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
585
      return(0);
586
    }
587
 
588
    // ----- Return
589
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
590
    return $p_list;
591
  }
592
  // --------------------------------------------------------------------------------
593
 
594
  // --------------------------------------------------------------------------------
595
  // Function :
596
  //   extract($p_path="./", $p_remove_path="")
597
  //   extract([$p_option, $p_option_value, ...])
598
  // Description :
599
  //   This method supports two synopsis. The first one is historical.
600
  //   This method extract all the files / directories from the archive to the
601
  //   folder indicated in $p_path.
602
  //   If you want to ignore the 'root' part of path of the memorized files
603
  //   you can indicate this in the optional $p_remove_path parameter.
604
  //   By default, if a newer file with the same name already exists, the
605
  //   file is not extracted.
606
  //
607
  //   If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
608
  //   are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
609
  //   at the end of the path value of PCLZIP_OPT_PATH.
610
  // Parameters :
611
  //   $p_path : Path where the files and directories are to be extracted
612
  //   $p_remove_path : First part ('root' part) of the memorized path
613
  //                    (if any similar) to remove while extracting.
614
  // Options :
615
  //   PCLZIP_OPT_PATH :
616
  //   PCLZIP_OPT_ADD_PATH :
617
  //   PCLZIP_OPT_REMOVE_PATH :
618
  //   PCLZIP_OPT_REMOVE_ALL_PATH :
619
  //   PCLZIP_CB_PRE_EXTRACT :
620
  //   PCLZIP_CB_POST_EXTRACT :
621
  // Return Values :
622
  //   0 or a negative value on failure,
623
  //   The list of the extracted files, with a status of the action.
624
  //   (see PclZip::listContent() for list entry format)
625
  // --------------------------------------------------------------------------------
626
  //function extract($p_path="./", $p_remove_path="")
627
  function extract(/* options */)
628
  {
629
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extract", "");
630
    $v_result=1;
631
 
632
    // ----- Reset the error handler
633
    $this->privErrorReset();
634
 
635
    // ----- Check archive
636
    if (!$this->privCheckFormat()) {
637
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
638
      return(0);
639
    }
640
 
641
    // ----- Set default values
642
    $v_options = array();
643
    $v_path = "./";
644
    $v_remove_path = "";
645
    $v_remove_all_path = false;
646
 
647
    // ----- Look for variable options arguments
648
    $v_size = func_num_args();
649
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
650
 
651
    // ----- Default values for option
652
    $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
653
 
654
    // ----- Look for arguments
655
    if ($v_size > 0) {
656
      // ----- Get the arguments
657
      $v_arg_list = &func_get_args();
658
 
659
      // ----- Look for first arg
660
      if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
661
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
662
 
663
        // ----- Parse the options
664
        $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
665
                                            array (PCLZIP_OPT_PATH => 'optional',
666
                                                   PCLZIP_OPT_REMOVE_PATH => 'optional',
667
                                                   PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
668
                                                   PCLZIP_OPT_ADD_PATH => 'optional',
669
                                                   PCLZIP_CB_PRE_EXTRACT => 'optional',
670
                                                   PCLZIP_CB_POST_EXTRACT => 'optional',
671
                                                   PCLZIP_OPT_SET_CHMOD => 'optional',
672
                                                   PCLZIP_OPT_BY_NAME => 'optional',
673
                                                   PCLZIP_OPT_BY_EREG => 'optional',
674
                                                   PCLZIP_OPT_BY_PREG => 'optional',
675
                                                   PCLZIP_OPT_BY_INDEX => 'optional',
676
                                                   PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
677
                                                   PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
678
                                                   PCLZIP_OPT_REPLACE_NEWER => 'optional'
679
                                                   ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
680
												    ));
681
        if ($v_result != 1) {
682
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
683
          return 0;
684
        }
685
 
686
        // ----- Set the arguments
687
        if (isset($v_options[PCLZIP_OPT_PATH])) {
688
          $v_path = $v_options[PCLZIP_OPT_PATH];
689
        }
690
        if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
691
          $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
692
        }
693
        if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
694
          $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
695
        }
696
        if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
697
          // ----- Check for '/' in last path char
698
          if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
699
            $v_path .= '/';
700
          }
701
          $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
702
        }
703
      }
704
 
705
      // ----- Look for 2 args
706
      // Here we need to support the first historic synopsis of the
707
      // method.
708
      else {
709
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
710
 
711
        // ----- Get the first argument
712
        $v_path = $v_arg_list[0];
713
 
714
        // ----- Look for the optional second argument
715
        if ($v_size == 2) {
716
          $v_remove_path = $v_arg_list[1];
717
        }
718
        else if ($v_size > 2) {
719
          // ----- Error log
720
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
721
 
722
          // ----- Return
723
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
724
          return 0;
725
        }
726
      }
727
    }
728
 
729
    // ----- Trace
730
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
731
 
732
    // ----- Call the extracting fct
733
    $p_list = array();
734
    $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
735
	                                     $v_remove_all_path, $v_options);
736
    if ($v_result < 1) {
737
      unset($p_list);
738
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
739
      return(0);
740
    }
741
 
742
    // ----- Return
743
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
744
    return $p_list;
745
  }
746
  // --------------------------------------------------------------------------------
747
 
748
 
749
  // --------------------------------------------------------------------------------
750
  // Function :
751
  //   extractByIndex($p_index, $p_path="./", $p_remove_path="")
752
  //   extractByIndex($p_index, [$p_option, $p_option_value, ...])
753
  // Description :
754
  //   This method supports two synopsis. The first one is historical.
755
  //   This method is doing a partial extract of the archive.
756
  //   The extracted files or folders are identified by their index in the
757
  //   archive (from 0 to n).
758
  //   Note that if the index identify a folder, only the folder entry is
759
  //   extracted, not all the files included in the archive.
760
  // Parameters :
761
  //   $p_index : A single index (integer) or a string of indexes of files to
762
  //              extract. The form of the string is "0,4-6,8-12" with only numbers
763
  //              and '-' for range or ',' to separate ranges. No spaces or ';'
764
  //              are allowed.
765
  //   $p_path : Path where the files and directories are to be extracted
766
  //   $p_remove_path : First part ('root' part) of the memorized path
767
  //                    (if any similar) to remove while extracting.
768
  // Options :
769
  //   PCLZIP_OPT_PATH :
770
  //   PCLZIP_OPT_ADD_PATH :
771
  //   PCLZIP_OPT_REMOVE_PATH :
772
  //   PCLZIP_OPT_REMOVE_ALL_PATH :
773
  //   PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
774
  //     not as files.
775
  //     The resulting content is in a new field 'content' in the file
776
  //     structure.
777
  //     This option must be used alone (any other options are ignored).
778
  //   PCLZIP_CB_PRE_EXTRACT :
779
  //   PCLZIP_CB_POST_EXTRACT :
780
  // Return Values :
781
  //   0 on failure,
782
  //   The list of the extracted files, with a status of the action.
783
  //   (see PclZip::listContent() for list entry format)
784
  // --------------------------------------------------------------------------------
785
  function extractByIndex($p_index /* $options */)
786
  {
787
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extractByIndex", "index='$p_index', ...");
788
    $v_result=1;
789
 
790
    // ----- Reset the error handler
791
    $this->privErrorReset();
792
 
793
    // ----- Check archive
794
    if (!$this->privCheckFormat()) {
795
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
796
      return(0);
797
    }
798
 
799
    // ----- Set default values
800
    $v_options = array();
801
    $v_path = "./";
802
    $v_remove_path = "";
803
    $v_remove_all_path = false;
804
 
805
    // ----- Look for variable options arguments
806
    $v_size = func_num_args();
807
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
808
 
809
    // ----- Default values for option
810
    $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
811
 
812
    // ----- Look for arguments
813
    if ($v_size > 1) {
814
      // ----- Get the arguments
815
      $v_arg_list = &func_get_args();
816
 
817
      // ----- Remove form the options list the first argument
818
      array_shift($v_arg_list);
819
      $v_size--;
820
 
821
      // ----- Look for first arg
822
      if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
823
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
824
 
825
        // ----- Parse the options
826
        $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
827
                                            array (PCLZIP_OPT_PATH => 'optional',
828
                                                   PCLZIP_OPT_REMOVE_PATH => 'optional',
829
                                                   PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
830
                                                   PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
831
                                                   PCLZIP_OPT_ADD_PATH => 'optional',
832
                                                   PCLZIP_CB_PRE_EXTRACT => 'optional',
833
                                                   PCLZIP_CB_POST_EXTRACT => 'optional',
834
                                                   PCLZIP_OPT_SET_CHMOD => 'optional',
835
                                                   PCLZIP_OPT_REPLACE_NEWER => 'optional'
836
                                                   ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
837
												   ));
838
        if ($v_result != 1) {
839
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
840
          return 0;
841
        }
842
 
843
        // ----- Set the arguments
844
        if (isset($v_options[PCLZIP_OPT_PATH])) {
845
          $v_path = $v_options[PCLZIP_OPT_PATH];
846
        }
847
        if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
848
          $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
849
        }
850
        if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
851
          $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
852
        }
853
        if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
854
          // ----- Check for '/' in last path char
855
          if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
856
            $v_path .= '/';
857
          }
858
          $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
859
        }
860
        if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
861
          $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
862
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING not set.");
863
        }
864
        else {
865
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING set.");
866
        }
867
      }
868
 
869
      // ----- Look for 2 args
870
      // Here we need to support the first historic synopsis of the
871
      // method.
872
      else {
873
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
874
 
875
        // ----- Get the first argument
876
        $v_path = $v_arg_list[0];
877
 
878
        // ----- Look for the optional second argument
879
        if ($v_size == 2) {
880
          $v_remove_path = $v_arg_list[1];
881
        }
882
        else if ($v_size > 2) {
883
          // ----- Error log
884
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
885
 
886
          // ----- Return
887
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
888
          return 0;
889
        }
890
      }
891
    }
892
 
893
    // ----- Trace
894
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "index='$p_index', path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
895
 
896
    // ----- Trick
897
    // Here I want to reuse extractByRule(), so I need to parse the $p_index
898
    // with privParseOptions()
899
    $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
900
    $v_options_trick = array();
901
    $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
902
                                        array (PCLZIP_OPT_BY_INDEX => 'optional' ));
903
    if ($v_result != 1) {
904
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
905
        return 0;
906
    }
907
    $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
908
 
909
    // ----- Call the extracting fct
910
    if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
911
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
912
        return(0);
913
    }
914
 
915
    // ----- Return
916
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
917
    return $p_list;
918
  }
919
  // --------------------------------------------------------------------------------
920
 
921
  // --------------------------------------------------------------------------------
922
  // Function :
923
  //   delete([$p_option, $p_option_value, ...])
924
  // Description :
925
  // Parameters :
926
  //   None
927
  // Options :
928
  //   PCLZIP_OPT_BY_INDEX :
929
  // Return Values :
930
  //   0 on failure,
931
  //   The list of the files which are still present in the archive.
932
  //   (see PclZip::listContent() for list entry format)
933
  // --------------------------------------------------------------------------------
934
  function delete(/* options */)
935
  {
936
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::delete", "");
937
    $v_result=1;
938
 
939
    // ----- Reset the error handler
940
    $this->privErrorReset();
941
 
942
    // ----- Check archive
943
    if (!$this->privCheckFormat()) {
944
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
945
      return(0);
946
    }
947
 
948
    // ----- Set default values
949
    $v_options = array();
950
 
951
    // ----- Look for variable options arguments
952
    $v_size = func_num_args();
953
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
954
 
955
    // ----- Look for no arguments
956
    if ($v_size <= 0) {
957
        // ----- Error log
958
        PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing arguments");
959
 
960
        // ----- Return
961
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
962
        return 0;
963
    }
964
 
965
    // ----- Get the arguments
966
    $v_arg_list = &func_get_args();
967
 
968
    // ----- Parse the options
969
    $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
970
                                        array (PCLZIP_OPT_BY_NAME => 'optional',
971
                                               PCLZIP_OPT_BY_EREG => 'optional',
972
                                               PCLZIP_OPT_BY_PREG => 'optional',
973
                                               PCLZIP_OPT_BY_INDEX => 'optional' ));
974
    if ($v_result != 1) {
975
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
976
        return 0;
977
    }
978
 
979
    // ----- Check that at least one rule is set
980
    if (   (!isset($v_options[PCLZIP_OPT_BY_NAME]))
981
        && (!isset($v_options[PCLZIP_OPT_BY_EREG]))
982
        && (!isset($v_options[PCLZIP_OPT_BY_PREG]))
983
        && (!isset($v_options[PCLZIP_OPT_BY_INDEX]))) {
984
        // ----- Error log
985
        PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "At least one filtering rule must be set");
986
 
987
        // ----- Return
988
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
989
        return 0;
990
    }
991
 
992
    // ----- Call the delete fct
993
    $v_list = array();
994
    if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1)
995
    {
996
      unset($v_list);
997
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
998
      return(0);
999
    }
1000
 
1001
    // ----- Return
1002
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_list);
1003
    return $v_list;
1004
  }
1005
  // --------------------------------------------------------------------------------
1006
 
1007
  // --------------------------------------------------------------------------------
1008
  // Function : deleteByIndex()
1009
  // Description :
1010
  //   ***** Deprecated *****
1011
  //   delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
1012
  // --------------------------------------------------------------------------------
1013
  function deleteByIndex($p_index)
1014
  {
1015
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::deleteByIndex", "index='$p_index'");
1016
 
1017
    $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
1018
 
1019
    // ----- Return
1020
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
1021
    return $p_list;
1022
  }
1023
  // --------------------------------------------------------------------------------
1024
 
1025
  // --------------------------------------------------------------------------------
1026
  // Function : properties()
1027
  // Description :
1028
  //   This method gives the properties of the archive.
1029
  //   The properties are :
1030
  //     nb : Number of files in the archive
1031
  //     comment : Comment associated with the archive file
1032
  //     status : not_exist, ok
1033
  // Parameters :
1034
  //   None
1035
  // Return Values :
1036
  //   0 on failure,
1037
  //   An array with the archive properties.
1038
  // --------------------------------------------------------------------------------
1039
  function properties()
1040
  {
1041
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::properties", "");
1042
 
1043
    // ----- Reset the error handler
1044
    $this->privErrorReset();
1045
 
1046
    // ----- Check archive
1047
    if (!$this->privCheckFormat()) {
1048
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1049
      return(0);
1050
    }
1051
 
1052
    // ----- Default properties
1053
    $v_prop = array();
1054
    $v_prop['comment'] = '';
1055
    $v_prop['nb'] = 0;
1056
    $v_prop['status'] = 'not_exist';
1057
 
1058
    // ----- Look if file exists
1059
    if (@is_file($this->zipname))
1060
    {
1061
      // ----- Open the zip file
1062
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
1063
      if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
1064
      {
1065
        // ----- Error log
1066
        PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
1067
 
1068
        // ----- Return
1069
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), 0);
1070
        return 0;
1071
      }
1072
 
1073
      // ----- Read the central directory informations
1074
      $v_central_dir = array();
1075
      if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
1076
      {
1077
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1078
        return 0;
1079
      }
1080
 
1081
      // ----- Close the zip file
1082
      $this->privCloseFd();
1083
 
1084
      // ----- Set the user attributes
1085
      $v_prop['comment'] = $v_central_dir['comment'];
1086
      $v_prop['nb'] = $v_central_dir['entries'];
1087
      $v_prop['status'] = 'ok';
1088
    }
1089
 
1090
    // ----- Return
1091
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_prop);
1092
    return $v_prop;
1093
  }
1094
  // --------------------------------------------------------------------------------
1095
 
1096
  // --------------------------------------------------------------------------------
1097
  // Function : duplicate()
1098
  // Description :
1099
  //   This method creates an archive by copying the content of an other one. If
1100
  //   the archive already exist, it is replaced by the new one without any warning.
1101
  // Parameters :
1102
  //   $p_archive : The filename of a valid archive, or
1103
  //                a valid PclZip object.
1104
  // Return Values :
1105
  //   1 on success.
1106
  //   0 or a negative value on error (error code).
1107
  // --------------------------------------------------------------------------------
1108
  function duplicate($p_archive)
1109
  {
1110
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::duplicate", "");
1111
    $v_result = 1;
1112
 
1113
    // ----- Reset the error handler
1114
    $this->privErrorReset();
1115
 
1116
    // ----- Look if the $p_archive is a PclZip object
1117
    if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
1118
    {
1119
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is valid PclZip object '".$p_archive->zipname."'");
1120
 
1121
      // ----- Duplicate the archive
1122
      $v_result = $this->privDuplicate($p_archive->zipname);
1123
    }
1124
 
1125
    // ----- Look if the $p_archive is a string (so a filename)
1126
    else if (is_string($p_archive))
1127
    {
1128
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is a filename '$p_archive'");
1129
 
1130
      // ----- Check that $p_archive is a valid zip file
1131
      // TBC : Should also check the archive format
1132
      if (!is_file($p_archive)) {
1133
        // ----- Error log
1134
        PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
1135
        $v_result = PCLZIP_ERR_MISSING_FILE;
1136
      }
1137
      else {
1138
        // ----- Duplicate the archive
1139
        $v_result = $this->privDuplicate($p_archive);
1140
      }
1141
    }
1142
 
1143
    // ----- Invalid variable
1144
    else
1145
    {
1146
      // ----- Error log
1147
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1148
      $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1149
    }
1150
 
1151
    // ----- Return
1152
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1153
    return $v_result;
1154
  }
1155
  // --------------------------------------------------------------------------------
1156
 
1157
  // --------------------------------------------------------------------------------
1158
  // Function : merge()
1159
  // Description :
1160
  //   This method merge the $p_archive_to_add archive at the end of the current
1161
  //   one ($this).
1162
  //   If the archive ($this) does not exist, the merge becomes a duplicate.
1163
  //   If the $p_archive_to_add archive does not exist, the merge is a success.
1164
  // Parameters :
1165
  //   $p_archive_to_add : It can be directly the filename of a valid zip archive,
1166
  //                       or a PclZip object archive.
1167
  // Return Values :
1168
  //   1 on success,
1169
  //   0 or negative values on error (see below).
1170
  // --------------------------------------------------------------------------------
1171
  function merge($p_archive_to_add)
1172
  {
1173
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::merge", "");
1174
    $v_result = 1;
1175
 
1176
    // ----- Reset the error handler
1177
    $this->privErrorReset();
1178
 
1179
    // ----- Check archive
1180
    if (!$this->privCheckFormat()) {
1181
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1182
      return(0);
1183
    }
1184
 
1185
    // ----- Look if the $p_archive_to_add is a PclZip object
1186
    if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
1187
    {
1188
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is valid PclZip object");
1189
 
1190
      // ----- Merge the archive
1191
      $v_result = $this->privMerge($p_archive_to_add);
1192
    }
1193
 
1194
    // ----- Look if the $p_archive_to_add is a string (so a filename)
1195
    else if (is_string($p_archive_to_add))
1196
    {
1197
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is a filename");
1198
 
1199
      // ----- Create a temporary archive
1200
      $v_object_archive = new PclZip($p_archive_to_add);
1201
 
1202
      // ----- Merge the archive
1203
      $v_result = $this->privMerge($v_object_archive);
1204
    }
1205
 
1206
    // ----- Invalid variable
1207
    else
1208
    {
1209
      // ----- Error log
1210
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1211
      $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1212
    }
1213
 
1214
    // ----- Return
1215
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1216
    return $v_result;
1217
  }
1218
  // --------------------------------------------------------------------------------
1219
 
1220
 
1221
 
1222
  // --------------------------------------------------------------------------------
1223
  // Function : errorCode()
1224
  // Description :
1225
  // Parameters :
1226
  // --------------------------------------------------------------------------------
1227
  function errorCode()
1228
  {
1229
    if (PCLZIP_ERROR_EXTERNAL == 1) {
1230
      return(PclErrorCode());
1231
    }
1232
    else {
1233
      return($this->error_code);
1234
    }
1235
  }
1236
  // --------------------------------------------------------------------------------
1237
 
1238
  // --------------------------------------------------------------------------------
1239
  // Function : errorName()
1240
  // Description :
1241
  // Parameters :
1242
  // --------------------------------------------------------------------------------
1243
  function errorName($p_with_code=false)
1244
  {
1245
    $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
1246
                      PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
1247
                      PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
1248
                      PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
1249
                      PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
1250
                      PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
1251
                      PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
1252
                      PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
1253
                      PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
1254
                      PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
1255
                      PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
1256
                      PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
1257
                      PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
1258
                      PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
1259
                      PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
1260
                      PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
1261
                      PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
1262
                      PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
1263
                      PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION' );
1264
 
1265
    if (isset($v_name[$this->error_code])) {
1266
      $v_value = $v_name[$this->error_code];
1267
    }
1268
    else {
1269
      $v_value = 'NoName';
1270
    }
1271
 
1272
    if ($p_with_code) {
1273
      return($v_value.' ('.$this->error_code.')');
1274
    }
1275
    else {
1276
      return($v_value);
1277
    }
1278
  }
1279
  // --------------------------------------------------------------------------------
1280
 
1281
  // --------------------------------------------------------------------------------
1282
  // Function : errorInfo()
1283
  // Description :
1284
  // Parameters :
1285
  // --------------------------------------------------------------------------------
1286
  function errorInfo($p_full=false)
1287
  {
1288
    if (PCLZIP_ERROR_EXTERNAL == 1) {
1289
      return(PclErrorString());
1290
    }
1291
    else {
1292
      if ($p_full) {
1293
        return($this->errorName(true)." : ".$this->error_string);
1294
      }
1295
      else {
1296
        return($this->error_string." [code ".$this->error_code."]");
1297
      }
1298
    }
1299
  }
1300
  // --------------------------------------------------------------------------------
1301
 
1302
 
1303
// --------------------------------------------------------------------------------
1304
// ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1305
// *****                                                        *****
1306
// *****       THESES FUNCTIONS MUST NOT BE USED DIRECTLY       *****
1307
// --------------------------------------------------------------------------------
1308
 
1309
 
1310
 
1311
  // --------------------------------------------------------------------------------
1312
  // Function : privCheckFormat()
1313
  // Description :
1314
  //   This method check that the archive exists and is a valid zip archive.
1315
  //   Several level of check exists. (futur)
1316
  // Parameters :
1317
  //   $p_level : Level of check. Default 0.
1318
  //              0 : Check the first bytes (magic codes) (default value))
1319
  //              1 : 0 + Check the central directory (futur)
1320
  //              2 : 1 + Check each file header (futur)
1321
  // Return Values :
1322
  //   true on success,
1323
  //   false on error, the error code is set.
1324
  // --------------------------------------------------------------------------------
1325
  function privCheckFormat($p_level=0)
1326
  {
1327
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFormat", "");
1328
    $v_result = true;
1329
 
1330
	// ----- Reset the file system cache
1331
    clearstatcache();
1332
 
1333
    // ----- Reset the error handler
1334
    $this->privErrorReset();
1335
 
1336
    // ----- Look if the file exits
1337
    if (!is_file($this->zipname)) {
1338
      // ----- Error log
1339
      PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
1340
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1341
      return(false);
1342
    }
1343
 
1344
    // ----- Check that the file is readeable
1345
    if (!is_readable($this->zipname)) {
1346
      // ----- Error log
1347
      PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
1348
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1349
      return(false);
1350
    }
1351
 
1352
    // ----- Check the magic code
1353
    // TBC
1354
 
1355
    // ----- Check the central header
1356
    // TBC
1357
 
1358
    // ----- Check each file header
1359
    // TBC
1360
 
1361
    // ----- Return
1362
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1363
    return $v_result;
1364
  }
1365
  // --------------------------------------------------------------------------------
1366
 
1367
  // --------------------------------------------------------------------------------
1368
  // Function : privParseOptions()
1369
  // Description :
1370
  //   This internal methods reads the variable list of arguments ($p_options_list,
1371
  //   $p_size) and generate an array with the options and values ($v_result_list).
1372
  //   $v_requested_options contains the options that can be present and those that
1373
  //   must be present.
1374
  //   $v_requested_options is an array, with the option value as key, and 'optional',
1375
  //   or 'mandatory' as value.
1376
  // Parameters :
1377
  //   See above.
1378
  // Return Values :
1379
  //   1 on success.
1380
  //   0 on failure.
1381
  // --------------------------------------------------------------------------------
1382
  function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
1383
  {
1384
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privParseOptions", "");
1385
    $v_result=1;
1386
 
1387
    // ----- Read the options
1388
    $i=0;
1389
    while ($i<$p_size) {
1390
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Looking for table index $i, option = '".PclZipUtilOptionText($p_options_list[$i])."(".$p_options_list[$i].")'");
1391
 
1392
      // ----- Check if the option is requested
1393
      if (!isset($v_requested_options[$p_options_list[$i]])) {
1394
        // ----- Error log
1395
        PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
1396
 
1397
        // ----- Return
1398
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1399
        return PclZip::errorCode();
1400
      }
1401
 
1402
      // ----- Look for next option
1403
      switch ($p_options_list[$i]) {
1404
        // ----- Look for options that request a path value
1405
        case PCLZIP_OPT_PATH :
1406
        case PCLZIP_OPT_REMOVE_PATH :
1407
        case PCLZIP_OPT_ADD_PATH :
1408
          // ----- Check the number of parameters
1409
          if (($i+1) >= $p_size) {
1410
            // ----- Error log
1411
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1412
 
1413
            // ----- Return
1414
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1415
            return PclZip::errorCode();
1416
          }
1417
 
1418
          // ----- Get the value
1419
          $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], false);
1420
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1421
          $i++;
1422
        break;
1423
 
1424
        // ----- Look for options that request an array of string for value
1425
        case PCLZIP_OPT_BY_NAME :
1426
          // ----- Check the number of parameters
1427
          if (($i+1) >= $p_size) {
1428
            // ----- Error log
1429
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1430
 
1431
            // ----- Return
1432
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1433
            return PclZip::errorCode();
1434
          }
1435
 
1436
          // ----- Get the value
1437
          if (is_string($p_options_list[$i+1])) {
1438
              $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
1439
          }
1440
          else if (is_array($p_options_list[$i+1])) {
1441
              $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1442
          }
1443
          else {
1444
            // ----- Error log
1445
            PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1446
 
1447
            // ----- Return
1448
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1449
            return PclZip::errorCode();
1450
          }
1451
          ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1452
          $i++;
1453
        break;
1454
 
1455
        // ----- Look for options that request an EREG or PREG expression
1456
        case PCLZIP_OPT_BY_EREG :
1457
        case PCLZIP_OPT_BY_PREG :
1458
        //case PCLZIP_OPT_CRYPT :
1459
          // ----- Check the number of parameters
1460
          if (($i+1) >= $p_size) {
1461
            // ----- Error log
1462
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1463
 
1464
            // ----- Return
1465
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1466
            return PclZip::errorCode();
1467
          }
1468
 
1469
          // ----- Get the value
1470
          if (is_string($p_options_list[$i+1])) {
1471
              $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1472
          }
1473
          else {
1474
            // ----- Error log
1475
            PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1476
 
1477
            // ----- Return
1478
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1479
            return PclZip::errorCode();
1480
          }
1481
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1482
          $i++;
1483
        break;
1484
 
1485
        // ----- Look for options that takes a string
1486
        case PCLZIP_OPT_COMMENT :
1487
        case PCLZIP_OPT_ADD_COMMENT :
1488
        case PCLZIP_OPT_PREPEND_COMMENT :
1489
          // ----- Check the number of parameters
1490
          if (($i+1) >= $p_size) {
1491
            // ----- Error log
1492
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
1493
			                     "Missing parameter value for option '"
1494
								 .PclZipUtilOptionText($p_options_list[$i])
1495
								 ."'");
1496
 
1497
            // ----- Return
1498
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1499
            return PclZip::errorCode();
1500
          }
1501
 
1502
          // ----- Get the value
1503
          if (is_string($p_options_list[$i+1])) {
1504
              $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1505
          }
1506
          else {
1507
            // ----- Error log
1508
            PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
1509
			                     "Wrong parameter value for option '"
1510
								 .PclZipUtilOptionText($p_options_list[$i])
1511
								 ."'");
1512
 
1513
            // ----- Return
1514
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1515
            return PclZip::errorCode();
1516
          }
1517
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1518
          $i++;
1519
        break;
1520
 
1521
        // ----- Look for options that request an array of index
1522
        case PCLZIP_OPT_BY_INDEX :
1523
          // ----- Check the number of parameters
1524
          if (($i+1) >= $p_size) {
1525
            // ----- Error log
1526
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1527
 
1528
            // ----- Return
1529
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1530
            return PclZip::errorCode();
1531
          }
1532
 
1533
          // ----- Get the value
1534
          $v_work_list = array();
1535
          if (is_string($p_options_list[$i+1])) {
1536
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is a string '".$p_options_list[$i+1]."'");
1537
 
1538
              // ----- Remove spaces
1539
              $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
1540
 
1541
              // ----- Parse items
1542
              $v_work_list = explode(",", $p_options_list[$i+1]);
1543
          }
1544
          else if (is_integer($p_options_list[$i+1])) {
1545
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an integer '".$p_options_list[$i+1]."'");
1546
              $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
1547
          }
1548
          else if (is_array($p_options_list[$i+1])) {
1549
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an array");
1550
              $v_work_list = $p_options_list[$i+1];
1551
          }
1552
          else {
1553
            // ----- Error log
1554
            PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1555
 
1556
            // ----- Return
1557
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1558
            return PclZip::errorCode();
1559
          }
1560
 
1561
          // ----- Reduce the index list
1562
          // each index item in the list must be a couple with a start and
1563
          // an end value : [0,3], [5-5], [8-10], ...
1564
          // ----- Check the format of each item
1565
          $v_sort_flag=false;
1566
          $v_sort_value=0;
1567
          for ($j=0; $j<sizeof($v_work_list); $j++) {
1568
              // ----- Explode the item
1569
              $v_item_list = explode("-", $v_work_list[$j]);
1570
              $v_size_item_list = sizeof($v_item_list);
1571
 
1572
              // ----- TBC : Here we might check that each item is a
1573
              // real integer ...
1574
 
1575
              // ----- Look for single value
1576
              if ($v_size_item_list == 1) {
1577
                  // ----- Set the option value
1578
                  $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1579
                  $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
1580
              }
1581
              elseif ($v_size_item_list == 2) {
1582
                  // ----- Set the option value
1583
                  $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1584
                  $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
1585
              }
1586
              else {
1587
                  // ----- Error log
1588
                  PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1589
 
1590
                  // ----- Return
1591
                  //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1592
                  return PclZip::errorCode();
1593
              }
1594
 
1595
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extracted index item = [".$v_result_list[$p_options_list[$i]][$j]['start'].",".$v_result_list[$p_options_list[$i]][$j]['end']."]");
1596
 
1597
              // ----- Look for list sort
1598
              if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
1599
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The list should be sorted ...");
1600
                  $v_sort_flag=true;
1601
 
1602
                  // ----- TBC : An automatic sort should be writen ...
1603
                  // ----- Error log
1604
                  PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1605
 
1606
                  // ----- Return
1607
                  //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1608
                  return PclZip::errorCode();
1609
              }
1610
              $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
1611
          }
1612
 
1613
          // ----- Sort the items
1614
          if ($v_sort_flag) {
1615
              // TBC : To Be Completed
1616
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "List sorting is not yet write ...");
1617
          }
1618
 
1619
          // ----- Next option
1620
          $i++;
1621
        break;
1622
 
1623
        // ----- Look for options that request no value
1624
        case PCLZIP_OPT_REMOVE_ALL_PATH :
1625
        case PCLZIP_OPT_EXTRACT_AS_STRING :
1626
        case PCLZIP_OPT_NO_COMPRESSION :
1627
        case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
1628
        case PCLZIP_OPT_REPLACE_NEWER :
1629
        case PCLZIP_OPT_STOP_ON_ERROR :
1630
          $v_result_list[$p_options_list[$i]] = true;
1631
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1632
        break;
1633
 
1634
        // ----- Look for options that request an octal value
1635
        case PCLZIP_OPT_SET_CHMOD :
1636
          // ----- Check the number of parameters
1637
          if (($i+1) >= $p_size) {
1638
            // ----- Error log
1639
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1640
 
1641
            // ----- Return
1642
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1643
            return PclZip::errorCode();
1644
          }
1645
 
1646
          // ----- Get the value
1647
          $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1648
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1649
          $i++;
1650
        break;
1651
 
1652
        // ----- Look for options that request a call-back
1653
        case PCLZIP_CB_PRE_EXTRACT :
1654
        case PCLZIP_CB_POST_EXTRACT :
1655
        case PCLZIP_CB_PRE_ADD :
1656
        case PCLZIP_CB_POST_ADD :
1657
        /* for futur use
1658
        case PCLZIP_CB_PRE_DELETE :
1659
        case PCLZIP_CB_POST_DELETE :
1660
        case PCLZIP_CB_PRE_LIST :
1661
        case PCLZIP_CB_POST_LIST :
1662
        */
1663
          // ----- Check the number of parameters
1664
          if (($i+1) >= $p_size) {
1665
            // ----- Error log
1666
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1667
 
1668
            // ----- Return
1669
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1670
            return PclZip::errorCode();
1671
          }
1672
 
1673
          // ----- Get the value
1674
          $v_function_name = $p_options_list[$i+1];
1675
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "call-back ".PclZipUtilOptionText($p_options_list[$i])." = '".$v_function_name."'");
1676
 
1677
          // ----- Check that the value is a valid existing function
1678
          if (!function_exists($v_function_name)) {
1679
            // ----- Error log
1680
            PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1681
 
1682
            // ----- Return
1683
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1684
            return PclZip::errorCode();
1685
          }
1686
 
1687
          // ----- Set the attribute
1688
          $v_result_list[$p_options_list[$i]] = $v_function_name;
1689
          $i++;
1690
        break;
1691
 
1692
        default :
1693
          // ----- Error log
1694
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
1695
		                       "Unknown parameter '"
1696
							   .$p_options_list[$i]."'");
1697
 
1698
          // ----- Return
1699
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1700
          return PclZip::errorCode();
1701
      }
1702
 
1703
      // ----- Next options
1704
      $i++;
1705
    }
1706
 
1707
    // ----- Look for mandatory options
1708
    if ($v_requested_options !== false) {
1709
      for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
1710
        // ----- Look for mandatory option
1711
        if ($v_requested_options[$key] == 'mandatory') {
1712
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
1713
          // ----- Look if present
1714
          if (!isset($v_result_list[$key])) {
1715
            // ----- Error log
1716
            PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
1717
 
1718
            // ----- Return
1719
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1720
            return PclZip::errorCode();
1721
          }
1722
        }
1723
      }
1724
    }
1725
 
1726
    // ----- Return
1727
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1728
    return $v_result;
1729
  }
1730
  // --------------------------------------------------------------------------------
1731
 
1732
  // --------------------------------------------------------------------------------
1733
  // Function : privCreate()
1734
  // Description :
1735
  // Parameters :
1736
  // Return Values :
1737
  // --------------------------------------------------------------------------------
1738
  function privCreate($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
1739
  {
1740
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCreate", "list, result_list, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1741
    $v_result=1;
1742
    $v_list_detail = array();
1743
 
1744
    // ----- Open the file in write mode
1745
    if (($v_result = $this->privOpenFd('wb')) != 1)
1746
    {
1747
      // ----- Return
1748
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1749
      return $v_result;
1750
    }
1751
 
1752
    // ----- Add the list of files
1753
    $v_result = $this->privAddList($p_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options);
1754
 
1755
    // ----- Close
1756
    $this->privCloseFd();
1757
 
1758
    // ----- Return
1759
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1760
    return $v_result;
1761
  }
1762
  // --------------------------------------------------------------------------------
1763
 
1764
  // --------------------------------------------------------------------------------
1765
  // Function : privAdd()
1766
  // Description :
1767
  // Parameters :
1768
  // Return Values :
1769
  // --------------------------------------------------------------------------------
1770
  function privAdd($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
1771
  {
1772
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAdd", "list, result_list, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
1773
    $v_result=1;
1774
    $v_list_detail = array();
1775
 
1776
    // ----- Look if the archive exists or is empty
1777
    if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
1778
    {
1779
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, or is empty, create it.");
1780
 
1781
      // ----- Do a create
1782
      $v_result = $this->privCreate($p_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options);
1783
 
1784
      // ----- Return
1785
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1786
      return $v_result;
1787
    }
1788
 
1789
    // ----- Open the zip file
1790
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
1791
    if (($v_result=$this->privOpenFd('rb')) != 1)
1792
    {
1793
      // ----- Return
1794
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1795
      return $v_result;
1796
    }
1797
 
1798
    // ----- Read the central directory informations
1799
    $v_central_dir = array();
1800
    if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
1801
    {
1802
      $this->privCloseFd();
1803
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1804
      return $v_result;
1805
    }
1806
 
1807
    // ----- Go to beginning of File
1808
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
1809
    @rewind($this->zip_fd);
1810
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
1811
 
1812
    // ----- Creates a temporay file
1813
    $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
1814
 
1815
    // ----- Open the temporary file in write mode
1816
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
1817
    if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
1818
    {
1819
      $this->privCloseFd();
1820
 
1821
      PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
1822
 
1823
      // ----- Return
1824
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1825
      return PclZip::errorCode();
1826
    }
1827
 
1828
    // ----- Copy the files from the archive to the temporary file
1829
    // TBC : Here I should better append the file and go back to erase the central dir
1830
    $v_size = $v_central_dir['offset'];
1831
    while ($v_size != 0)
1832
    {
1833
      $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
1834
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
1835
      $v_buffer = fread($this->zip_fd, $v_read_size);
1836
      @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
1837
      $v_size -= $v_read_size;
1838
    }
1839
 
1840
    // ----- Swap the file descriptor
1841
    // Here is a trick : I swap the temporary fd with the zip fd, in order to use
1842
    // the following methods on the temporary fil and not the real archive
1843
    $v_swap = $this->zip_fd;
1844
    $this->zip_fd = $v_zip_temp_fd;
1845
    $v_zip_temp_fd = $v_swap;
1846
 
1847
    // ----- Add the files
1848
    $v_header_list = array();
1849
    if (($v_result = $this->privAddFileList($p_list, $v_header_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1)
1850
    {
1851
      fclose($v_zip_temp_fd);
1852
      $this->privCloseFd();
1853
      @unlink($v_zip_temp_name);
1854
 
1855
      // ----- Return
1856
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1857
      return $v_result;
1858
    }
1859
 
1860
    // ----- Store the offset of the central dir
1861
    $v_offset = @ftell($this->zip_fd);
1862
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
1863
 
1864
    // ----- Copy the block of file headers from the old archive
1865
    $v_size = $v_central_dir['size'];
1866
    while ($v_size != 0)
1867
    {
1868
      $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
1869
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
1870
      $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
1871
      @fwrite($this->zip_fd, $v_buffer, $v_read_size);
1872
      $v_size -= $v_read_size;
1873
    }
1874
 
1875
    // ----- Create the Central Dir files header
1876
    for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++)
1877
    {
1878
      // ----- Create the file header
1879
      if ($v_header_list[$i]['status'] == 'ok') {
1880
        if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
1881
          fclose($v_zip_temp_fd);
1882
          $this->privCloseFd();
1883
          @unlink($v_zip_temp_name);
1884
 
1885
          // ----- Return
1886
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1887
          return $v_result;
1888
        }
1889
        $v_count++;
1890
      }
1891
 
1892
      // ----- Transform the header to a 'usable' info
1893
      $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
1894
    }
1895
 
1896
    // ----- Zip file comment
1897
    $v_comment = $v_central_dir['comment'];
1898
    if (isset($p_options[PCLZIP_OPT_COMMENT])) {
1899
      $v_comment = $p_options[PCLZIP_OPT_COMMENT];
1900
    }
1901
    if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
1902
      $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
1903
    }
1904
    if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
1905
      $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
1906
    }
1907
 
1908
    // ----- Calculate the size of the central header
1909
    $v_size = @ftell($this->zip_fd)-$v_offset;
1910
 
1911
    // ----- Create the central dir footer
1912
    if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
1913
    {
1914
      // ----- Reset the file list
1915
      unset($v_header_list);
1916
 
1917
      // ----- Return
1918
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1919
      return $v_result;
1920
    }
1921
 
1922
    // ----- Swap back the file descriptor
1923
    $v_swap = $this->zip_fd;
1924
    $this->zip_fd = $v_zip_temp_fd;
1925
    $v_zip_temp_fd = $v_swap;
1926
 
1927
    // ----- Close
1928
    $this->privCloseFd();
1929
 
1930
    // ----- Close the temporary file
1931
    @fclose($v_zip_temp_fd);
1932
 
1933
    // ----- Delete the zip file
1934
    // TBC : I should test the result ...
1935
    @unlink($this->zipname);
1936
 
1937
    // ----- Rename the temporary file
1938
    // TBC : I should test the result ...
1939
    //@rename($v_zip_temp_name, $this->zipname);
1940
    PclZipUtilRename($v_zip_temp_name, $this->zipname);
1941
 
1942
    // ----- Return
1943
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1944
    return $v_result;
1945
  }
1946
  // --------------------------------------------------------------------------------
1947
 
1948
  // --------------------------------------------------------------------------------
1949
  // Function : privOpenFd()
1950
  // Description :
1951
  // Parameters :
1952
  // --------------------------------------------------------------------------------
1953
  function privOpenFd($p_mode)
1954
  {
1955
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privOpenFd", 'mode='.$p_mode);
1956
    $v_result=1;
1957
 
1958
    // ----- Look if already open
1959
    if ($this->zip_fd != 0)
1960
    {
1961
      // ----- Error log
1962
      PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
1963
 
1964
      // ----- Return
1965
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1966
      return PclZip::errorCode();
1967
    }
1968
 
1969
    // ----- Open the zip file
1970
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Open file in '.$p_mode.' mode');
1971
    if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
1972
    {
1973
      // ----- Error log
1974
      PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
1975
 
1976
      // ----- Return
1977
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1978
      return PclZip::errorCode();
1979
    }
1980
 
1981
    // ----- Return
1982
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1983
    return $v_result;
1984
  }
1985
  // --------------------------------------------------------------------------------
1986
 
1987
  // --------------------------------------------------------------------------------
1988
  // Function : privCloseFd()
1989
  // Description :
1990
  // Parameters :
1991
  // --------------------------------------------------------------------------------
1992
  function privCloseFd()
1993
  {
1994
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCloseFd", "");
1995
    $v_result=1;
1996
 
1997
    if ($this->zip_fd != 0)
1998
      @fclose($this->zip_fd);
1999
    $this->zip_fd = 0;
2000
 
2001
    // ----- Return
2002
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2003
    return $v_result;
2004
  }
2005
  // --------------------------------------------------------------------------------
2006
 
2007
  // --------------------------------------------------------------------------------
2008
  // Function : privAddList()
2009
  // Description :
2010
  //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
2011
  //   different from the real path of the file. This is usefull if you want to have PclTar
2012
  //   running in any directory, and memorize relative path from an other directory.
2013
  // Parameters :
2014
  //   $p_list : An array containing the file or directory names to add in the tar
2015
  //   $p_result_list : list of added files with their properties (specially the status field)
2016
  //   $p_add_dir : Path to add in the filename path archived
2017
  //   $p_remove_dir : Path to remove in the filename path archived
2018
  // Return Values :
2019
  // --------------------------------------------------------------------------------
2020
  function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
2021
  {
2022
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddList", "list, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
2023
    $v_result=1;
2024
 
2025
    // ----- Add the files
2026
    $v_header_list = array();
2027
    if (($v_result = $this->privAddFileList($p_list, $v_header_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1)
2028
    {
2029
      // ----- Return
2030
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2031
      return $v_result;
2032
    }
2033
 
2034
    // ----- Store the offset of the central dir
2035
    $v_offset = @ftell($this->zip_fd);
2036
 
2037
    // ----- Create the Central Dir files header
2038
    for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
2039
    {
2040
      // ----- Create the file header
2041
      if ($v_header_list[$i]['status'] == 'ok') {
2042
        if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2043
          // ----- Return
2044
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2045
          return $v_result;
2046
        }
2047
        $v_count++;
2048
      }
2049
 
2050
      // ----- Transform the header to a 'usable' info
2051
      $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2052
    }
2053
 
2054
    // ----- Zip file comment
2055
    $v_comment = '';
2056
    if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2057
      $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2058
    }
2059
 
2060
    // ----- Calculate the size of the central header
2061
    $v_size = @ftell($this->zip_fd)-$v_offset;
2062
 
2063
    // ----- Create the central dir footer
2064
    if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
2065
    {
2066
      // ----- Reset the file list
2067
      unset($v_header_list);
2068
 
2069
      // ----- Return
2070
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2071
      return $v_result;
2072
    }
2073
 
2074
    // ----- Return
2075
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2076
    return $v_result;
2077
  }
2078
  // --------------------------------------------------------------------------------
2079
 
2080
  // --------------------------------------------------------------------------------
2081
  // Function : privAddFileList()
2082
  // Description :
2083
  //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
2084
  //   different from the real path of the file. This is usefull if you want to
2085
  //   run the lib in any directory, and memorize relative path from an other directory.
2086
  // Parameters :
2087
  //   $p_list : An array containing the file or directory names to add in the tar
2088
  //   $p_result_list : list of added files with their properties (specially the status field)
2089
  //   $p_add_dir : Path to add in the filename path archived
2090
  //   $p_remove_dir : Path to remove in the filename path archived
2091
  // Return Values :
2092
  // --------------------------------------------------------------------------------
2093
  function privAddFileList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
2094
  {
2095
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFileList", "list, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
2096
    $v_result=1;
2097
    $v_header = array();
2098
 
2099
    // ----- Recuperate the current number of elt in list
2100
    $v_nb = sizeof($p_result_list);
2101
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Before add, list have $v_nb elements");
2102
 
2103
    // ----- Loop on the files
2104
    for ($j=0; ($j<count($p_list)) && ($v_result==1); $j++)
2105
    {
2106
      // ----- Recuperate the filename
2107
      $p_filename = PclZipUtilTranslateWinPath($p_list[$j], false);
2108
 
2109
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for file [$p_filename]");
2110
 
2111
      // ----- Skip empty file names
2112
      if ($p_filename == "")
2113
      {
2114
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Skip empty filename");
2115
        continue;
2116
      }
2117
 
2118
      // ----- Check the filename
2119
      if (!file_exists($p_filename))
2120
      {
2121
        // ----- Error log
2122
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '$p_filename' does not exists");
2123
        PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '$p_filename' does not exists");
2124
 
2125
        // ----- Return
2126
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2127
        return PclZip::errorCode();
2128
      }
2129
 
2130
      /* This test is done later
2131
      // ----- Check the path length
2132
      if (strlen($p_filename) > 0xFF)
2133
      {
2134
        // ----- Error log
2135
        PclZip::privErrorLog(-5, "File name is too long (max. 255) : '$p_filename'");
2136
 
2137
        // ----- Return
2138
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2139
        return PclZip::errorCode();
2140
      }
2141
      */
2142
 
2143
      // ----- Look if it is a file or a dir with no all pathnre move
2144
      if ((is_file($p_filename)) || ((is_dir($p_filename)) && !$p_remove_all_dir)) {
2145
        // ----- Add the file
2146
        if (($v_result = $this->privAddFile($p_filename, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1)
2147
        {
2148
          // ----- Return status
2149
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2150
          return $v_result;
2151
        }
2152
 
2153
        // ----- Store the file infos
2154
        $p_result_list[$v_nb++] = $v_header;
2155
      }
2156
 
2157
      // ----- Look for directory
2158
      if (@is_dir($p_filename))
2159
      {
2160
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "$p_filename is a directory");
2161
 
2162
        // ----- Look for path
2163
        if ($p_filename != ".")
2164
          $v_path = $p_filename."/";
2165
        else
2166
          $v_path = "";
2167
 
2168
        // ----- Read the directory for files and sub-directories
2169
        if ($p_hdir = @opendir($p_filename)) {
2170
          $p_hitem = @readdir($p_hdir); // '.' directory
2171
          $p_hitem = @readdir($p_hdir); // '..' directory
2172
          while (($p_hitem = @readdir($p_hdir)) !== false) {
2173
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for $p_hitem in the directory");
2174
 
2175
            // ----- Look for a file
2176
            if (is_file($v_path.$p_hitem)) {
2177
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Add the file '".$v_path.$p_hitem."'");
2178
 
2179
              // ----- Add the file
2180
              if (($v_result = $this->privAddFile($v_path.$p_hitem, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1) {
2181
                // ----- Return status
2182
                //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2183
                return $v_result;
2184
              }
2185
 
2186
              // ----- Store the file infos
2187
              $p_result_list[$v_nb++] = $v_header;
2188
            }
2189
 
2190
            // ----- Recursive call to privAddFileList()
2191
            else {
2192
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Add the directory '".$v_path.$p_hitem."'");
2193
 
2194
              // ----- Need an array as parameter
2195
              $p_temp_list[0] = $v_path.$p_hitem;
2196
              $v_result = $this->privAddFileList($p_temp_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options);
2197
 
2198
              // ----- Update the number of elements of the list
2199
              $v_nb = sizeof($p_result_list);
2200
            }
2201
          }
2202
        }
2203
 
2204
        // ----- Free memory for the recursive loop
2205
        unset($p_temp_list);
2206
        unset($p_hdir);
2207
        unset($p_hitem);
2208
      }
2209
    }
2210
 
2211
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "After add, list have $v_nb elements");
2212
 
2213
    // ----- Return
2214
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2215
    return $v_result;
2216
  }
2217
  // --------------------------------------------------------------------------------
2218
 
2219
  // --------------------------------------------------------------------------------
2220
  // Function : privAddFile()
2221
  // Description :
2222
  // Parameters :
2223
  // Return Values :
2224
  // --------------------------------------------------------------------------------
2225
  function privAddFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
2226
  {
2227
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFile", "filename='$p_filename', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
2228
    $v_result=1;
2229
 
2230
    if ($p_filename == "")
2231
    {
2232
      // ----- Error log
2233
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
2234
 
2235
      // ----- Return
2236
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2237
      return PclZip::errorCode();
2238
    }
2239
 
2240
    // ----- Calculate the stored filename
2241
    $v_stored_filename = $p_filename;
2242
 
2243
    // ----- Look for all path to remove
2244
    if ($p_remove_all_dir) {
2245
      $v_stored_filename = basename($p_filename);
2246
    }
2247
    // ----- Look for partial path remove
2248
    else if ($p_remove_dir != "")
2249
    {
2250
      if (substr($p_remove_dir, -1) != '/')
2251
        $p_remove_dir .= "/";
2252
 
2253
      if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./"))
2254
      {
2255
        if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./"))
2256
          $p_remove_dir = "./".$p_remove_dir;
2257
        if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./"))
2258
          $p_remove_dir = substr($p_remove_dir, 2);
2259
      }
2260
 
2261
      $v_compare = PclZipUtilPathInclusion($p_remove_dir, $p_filename);
2262
      if ($v_compare > 0)
2263
//      if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
2264
      {
2265
 
2266
        if ($v_compare == 2) {
2267
          $v_stored_filename = "";
2268
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Path to remove is the current folder");
2269
        }
2270
        else {
2271
          $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
2272
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove path '$p_remove_dir' in file '$p_filename' = '$v_stored_filename'");
2273
        }
2274
      }
2275
    }
2276
    // ----- Look for path to add
2277
    if ($p_add_dir != "")
2278
    {
2279
      if (substr($p_add_dir, -1) == "/")
2280
        $v_stored_filename = $p_add_dir.$v_stored_filename;
2281
      else
2282
        $v_stored_filename = $p_add_dir."/".$v_stored_filename;
2283
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
2284
    }
2285
 
2286
    // ----- Filename (reduce the path of stored name)
2287
    $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
2288
 
2289
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Filename (reduced) '$v_stored_filename', strlen ".strlen($v_stored_filename));
2290
 
2291
    // ----- Set the file properties
2292
    clearstatcache();
2293
    $p_header['version'] = 20;
2294
    $p_header['version_extracted'] = 10;
2295
    $p_header['flag'] = 0;
2296
    $p_header['compression'] = 0;
2297
    $p_header['mtime'] = filemtime($p_filename);
2298
    $p_header['crc'] = 0;
2299
    $p_header['compressed_size'] = 0;
2300
    $p_header['size'] = filesize($p_filename);
2301
    $p_header['filename_len'] = strlen($p_filename);
2302
    $p_header['extra_len'] = 0;
2303
    $p_header['comment_len'] = 0;
2304
    $p_header['disk'] = 0;
2305
    $p_header['internal'] = 0;
2306
//    $p_header['external'] = (is_file($p_filename)?0xFE49FFE0:0x41FF0010);
2307
    $p_header['external'] = (is_file($p_filename)?0x00000000:0x00000010);
2308
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header external extension '".sprintf("0x%X",$p_header['external'])."'");
2309
    $p_header['offset'] = 0;
2310
    $p_header['filename'] = $p_filename;
2311
    $p_header['stored_filename'] = $v_stored_filename;
2312
    $p_header['extra'] = '';
2313
    $p_header['comment'] = '';
2314
    $p_header['status'] = 'ok';
2315
    $p_header['index'] = -1;
2316
 
2317
 
2318
    // ----- Look for pre-add callback
2319
    if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
2320
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_ADD]."()') is defined for the extraction");
2321
 
2322
      // ----- Generate a local information
2323
      $v_local_header = array();
2324
      $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2325
 
2326
      // ----- Call the callback
2327
      // Here I do not use call_user_func() because I need to send a reference to the
2328
      // header.
2329
      eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
2330
      if ($v_result == 0) {
2331
        // ----- Change the file status
2332
        $p_header['status'] = "skipped";
2333
        $v_result = 1;
2334
      }
2335
 
2336
      // ----- Update the informations
2337
      // Only some fields can be modified
2338
      if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
2339
        $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
2340
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New stored filename is '".$p_header['stored_filename']."'");
2341
      }
2342
    }
2343
 
2344
    // ----- Look for empty stored filename
2345
    if ($p_header['stored_filename'] == "") {
2346
      $p_header['status'] = "filtered";
2347
    }
2348
 
2349
    // ----- Check the path length
2350
    if (strlen($p_header['stored_filename']) > 0xFF) {
2351
      $p_header['status'] = 'filename_too_long';
2352
    }
2353
 
2354
    // ----- Look if no error, or file not skipped
2355
    if ($p_header['status'] == 'ok') {
2356
 
2357
      // ----- Look for a file
2358
      if (is_file($p_filename))
2359
      {
2360
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a file");
2361
        // ----- Open the source file
2362
        if (($v_file = @fopen($p_filename, "rb")) == 0) {
2363
          PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
2364
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2365
          return PclZip::errorCode();
2366
        }
2367
 
2368
        if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
2369
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be compressed");
2370
          // ----- Read the file content
2371
          $v_content_compressed = @fread($v_file, $p_header['size']);
2372
 
2373
          // ----- Calculate the CRC
2374
          $p_header['crc'] = @crc32($v_content_compressed);
2375
 
2376
          // ----- Set header parameters
2377
          $p_header['compressed_size'] = $p_header['size'];
2378
          $p_header['compression'] = 0;
2379
        }
2380
        else {
2381
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will be compressed");
2382
          // ----- Read the file content
2383
          $v_content = @fread($v_file, $p_header['size']);
2384
 
2385
          // ----- Calculate the CRC
2386
          $p_header['crc'] = @crc32($v_content);
2387
 
2388
          // ----- Compress the file
2389
          $v_content_compressed = @gzdeflate($v_content);
2390
 
2391
          // ----- Set header parameters
2392
          $p_header['compressed_size'] = strlen($v_content_compressed);
2393
          $p_header['compression'] = 8;
2394
        }
2395
 
2396
        // ----- Look for encryption
2397
        /*
2398
        if ((isset($p_options[PCLZIP_OPT_CRYPT]))
2399
		    && ($p_options[PCLZIP_OPT_CRYPT] != "")) {
2400
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File need to be crypted ....");
2401
 
2402
          // Should be a random header
2403
          $v_header = 'xxxxxxxxxxxx';
2404
	      $v_content_compressed = PclZipUtilZipEncrypt($v_content_compressed,
2405
		                                           $p_header['compressed_size'],
2406
	                                               $v_header,
2407
												   $p_header['crc'],
2408
												   "test");
2409
 
2410
          $p_header['compressed_size'] += 12;
2411
          $p_header['flag'] = 1;
2412
 
2413
          // ----- Add the header to the data
2414
          $v_content_compressed = $v_header.$v_content_compressed;
2415
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size after header : ".strlen($v_content_compressed)."");
2416
        }
2417
        */
2418
 
2419
        // ----- Call the header generation
2420
        if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2421
          @fclose($v_file);
2422
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2423
          return $v_result;
2424
        }
2425
 
2426
        // ----- Write the compressed (or not) content
2427
        $v_binary_data = pack('a'.$p_header['compressed_size'],
2428
		                      $v_content_compressed);
2429
        @fwrite($this->zip_fd, $v_binary_data, $p_header['compressed_size']);
2430
 
2431
        // ----- Close the file
2432
        @fclose($v_file);
2433
      }
2434
 
2435
      // ----- Look for a directory
2436
      else {
2437
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a folder");
2438
        // ----- Look for directory last '/'
2439
        if (@substr($p_header['stored_filename'], -1) != '/') {
2440
          $p_header['stored_filename'] .= '/';
2441
        }
2442
 
2443
        // ----- Set the file properties
2444
        $p_header['size'] = 0;
2445
        //$p_header['external'] = 0x41FF0010;   // Value for a folder : to be checked
2446
        $p_header['external'] = 0x00000010;   // Value for a folder : to be checked
2447
 
2448
        // ----- Call the header generation
2449
        if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
2450
        {
2451
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2452
          return $v_result;
2453
        }
2454
      }
2455
    }
2456
 
2457
    // ----- Look for pre-add callback
2458
    if (isset($p_options[PCLZIP_CB_POST_ADD])) {
2459
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_ADD]."()') is defined for the extraction");
2460
 
2461
      // ----- Generate a local information
2462
      $v_local_header = array();
2463
      $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2464
 
2465
      // ----- Call the callback
2466
      // Here I do not use call_user_func() because I need to send a reference to the
2467
      // header.
2468
      eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
2469
      if ($v_result == 0) {
2470
        // ----- Ignored
2471
        $v_result = 1;
2472
      }
2473
 
2474
      // ----- Update the informations
2475
      // Nothing can be modified
2476
    }
2477
 
2478
    // ----- Return
2479
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2480
    return $v_result;
2481
  }
2482
  // --------------------------------------------------------------------------------
2483
 
2484
  // --------------------------------------------------------------------------------
2485
  // Function : privWriteFileHeader()
2486
  // Description :
2487
  // Parameters :
2488
  // Return Values :
2489
  // --------------------------------------------------------------------------------
2490
  function privWriteFileHeader(&$p_header)
2491
  {
2492
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
2493
    $v_result=1;
2494
 
2495
    // TBC
2496
    //for(reset($p_header); $key = key($p_header); next($p_header)) {
2497
    //  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "header[$key] = ".$p_header[$key]);
2498
    //}
2499
 
2500
    // ----- Store the offset position of the file
2501
    $p_header['offset'] = ftell($this->zip_fd);
2502
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'File offset of the header :'.$p_header['offset']);
2503
 
2504
    // ----- Transform UNIX mtime to DOS format mdate/mtime
2505
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
2506
    $v_date = getdate($p_header['mtime']);
2507
    $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
2508
    $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
2509
 
2510
    // ----- Packed data
2511
    $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
2512
	                      $p_header['version_extracted'], $p_header['flag'],
2513
                          $p_header['compression'], $v_mtime, $v_mdate,
2514
                          $p_header['crc'], $p_header['compressed_size'],
2515
						  $p_header['size'],
2516
                          strlen($p_header['stored_filename']),
2517
						  $p_header['extra_len']);
2518
 
2519
    // ----- Write the first 148 bytes of the header in the archive
2520
    fputs($this->zip_fd, $v_binary_data, 30);
2521
 
2522
    // ----- Write the variable fields
2523
    if (strlen($p_header['stored_filename']) != 0)
2524
    {
2525
      fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
2526
    }
2527
    if ($p_header['extra_len'] != 0)
2528
    {
2529
      fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
2530
    }
2531
 
2532
    // ----- Return
2533
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2534
    return $v_result;
2535
  }
2536
  // --------------------------------------------------------------------------------
2537
 
2538
  // --------------------------------------------------------------------------------
2539
  // Function : privWriteCentralFileHeader()
2540
  // Description :
2541
  // Parameters :
2542
  // Return Values :
2543
  // --------------------------------------------------------------------------------
2544
  function privWriteCentralFileHeader(&$p_header)
2545
  {
2546
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
2547
    $v_result=1;
2548
 
2549
    // TBC
2550
    //for(reset($p_header); $key = key($p_header); next($p_header)) {
2551
    //  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "header[$key] = ".$p_header[$key]);
2552
    //}
2553
 
2554
    // ----- Transform UNIX mtime to DOS format mdate/mtime
2555
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
2556
    $v_date = getdate($p_header['mtime']);
2557
    $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
2558
    $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
2559
 
2560
    // ----- Packed data
2561
    $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
2562
	                      $p_header['version'], $p_header['version_extracted'],
2563
                          $p_header['flag'], $p_header['compression'],
2564
						  $v_mtime, $v_mdate, $p_header['crc'],
2565
                          $p_header['compressed_size'], $p_header['size'],
2566
                          strlen($p_header['stored_filename']),
2567
						  $p_header['extra_len'], $p_header['comment_len'],
2568
                          $p_header['disk'], $p_header['internal'],
2569
						  $p_header['external'], $p_header['offset']);
2570
 
2571
    // ----- Write the 42 bytes of the header in the zip file
2572
    fputs($this->zip_fd, $v_binary_data, 46);
2573
 
2574
    // ----- Write the variable fields
2575
    if (strlen($p_header['stored_filename']) != 0)
2576
    {
2577
      fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
2578
    }
2579
    if ($p_header['extra_len'] != 0)
2580
    {
2581
      fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
2582
    }
2583
    if ($p_header['comment_len'] != 0)
2584
    {
2585
      fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
2586
    }
2587
 
2588
    // ----- Return
2589
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2590
    return $v_result;
2591
  }
2592
  // --------------------------------------------------------------------------------
2593
 
2594
  // --------------------------------------------------------------------------------
2595
  // Function : privWriteCentralHeader()
2596
  // Description :
2597
  // Parameters :
2598
  // Return Values :
2599
  // --------------------------------------------------------------------------------
2600
  function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
2601
  {
2602
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralHeader", 'nb_entries='.$p_nb_entries.', size='.$p_size.', offset='.$p_offset.', comment="'.$p_comment.'"');
2603
    $v_result=1;
2604
 
2605
    // ----- Packed data
2606
    $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
2607
	                      $p_nb_entries, $p_size,
2608
						  $p_offset, strlen($p_comment));
2609
 
2610
    // ----- Write the 22 bytes of the header in the zip file
2611
    fputs($this->zip_fd, $v_binary_data, 22);
2612
 
2613
    // ----- Write the variable fields
2614
    if (strlen($p_comment) != 0)
2615
    {
2616
      fputs($this->zip_fd, $p_comment, strlen($p_comment));
2617
    }
2618
 
2619
    // ----- Return
2620
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2621
    return $v_result;
2622
  }
2623
  // --------------------------------------------------------------------------------
2624
 
2625
  // --------------------------------------------------------------------------------
2626
  // Function : privList()
2627
  // Description :
2628
  // Parameters :
2629
  // Return Values :
2630
  // --------------------------------------------------------------------------------
2631
  function privList(&$p_list)
2632
  {
2633
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privList", "list");
2634
    $v_result=1;
2635
 
2636
    // ----- Open the zip file
2637
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
2638
    if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
2639
    {
2640
      // ----- Error log
2641
      PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
2642
 
2643
      // ----- Return
2644
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2645
      return PclZip::errorCode();
2646
    }
2647
 
2648
    // ----- Read the central directory informations
2649
    $v_central_dir = array();
2650
    if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
2651
    {
2652
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2653
      return $v_result;
2654
    }
2655
 
2656
    // ----- Go to beginning of Central Dir
2657
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Offset : ".$v_central_dir['offset']."'");
2658
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
2659
    @rewind($this->zip_fd);
2660
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
2661
    if (@fseek($this->zip_fd, $v_central_dir['offset']))
2662
    {
2663
      // ----- Error log
2664
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
2665
 
2666
      // ----- Return
2667
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2668
      return PclZip::errorCode();
2669
    }
2670
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
2671
 
2672
    // ----- Read each entry
2673
    for ($i=0; $i<$v_central_dir['entries']; $i++)
2674
    {
2675
      // ----- Read the file header
2676
      if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
2677
      {
2678
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2679
        return $v_result;
2680
      }
2681
      $v_header['index'] = $i;
2682
 
2683
      // ----- Get the only interesting attributes
2684
      $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
2685
      unset($v_header);
2686
    }
2687
 
2688
    // ----- Close the zip file
2689
    $this->privCloseFd();
2690
 
2691
    // ----- Return
2692
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2693
    return $v_result;
2694
  }
2695
  // --------------------------------------------------------------------------------
2696
 
2697
  // --------------------------------------------------------------------------------
2698
  // Function : privConvertHeader2FileInfo()
2699
  // Description :
2700
  //   This function takes the file informations from the central directory
2701
  //   entries and extract the interesting parameters that will be given back.
2702
  //   The resulting file infos are set in the array $p_info
2703
  //     $p_info['filename'] : Filename with full path. Given by user (add),
2704
  //                           extracted in the filesystem (extract).
2705
  //     $p_info['stored_filename'] : Stored filename in the archive.
2706
  //     $p_info['size'] = Size of the file.
2707
  //     $p_info['compressed_size'] = Compressed size of the file.
2708
  //     $p_info['mtime'] = Last modification date of the file.
2709
  //     $p_info['comment'] = Comment associated with the file.
2710
  //     $p_info['folder'] = true/false : indicates if the entry is a folder or not.
2711
  //     $p_info['status'] = status of the action on the file.
2712
  // Parameters :
2713
  // Return Values :
2714
  // --------------------------------------------------------------------------------
2715
  function privConvertHeader2FileInfo($p_header, &$p_info)
2716
  {
2717
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privConvertHeader2FileInfo", "Filename='".$p_header['filename']."'");
2718
    $v_result=1;
2719
 
2720
    // ----- Get the interesting attributes
2721
    $p_info['filename'] = $p_header['filename'];
2722
    $p_info['stored_filename'] = $p_header['stored_filename'];
2723
    $p_info['size'] = $p_header['size'];
2724
    $p_info['compressed_size'] = $p_header['compressed_size'];
2725
    $p_info['mtime'] = $p_header['mtime'];
2726
    $p_info['comment'] = $p_header['comment'];
2727
    $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
2728
    $p_info['index'] = $p_header['index'];
2729
    $p_info['status'] = $p_header['status'];
2730
 
2731
    // ----- Return
2732
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2733
    return $v_result;
2734
  }
2735
  // --------------------------------------------------------------------------------
2736
 
2737
  // --------------------------------------------------------------------------------
2738
  // Function : privExtractByRule()
2739
  // Description :
2740
  //   Extract a file or directory depending of rules (by index, by name, ...)
2741
  // Parameters :
2742
  //   $p_file_list : An array where will be placed the properties of each
2743
  //                  extracted file
2744
  //   $p_path : Path to add while writing the extracted files
2745
  //   $p_remove_path : Path to remove (from the file memorized path) while writing the
2746
  //                    extracted files. If the path does not match the file path,
2747
  //                    the file is extracted with its memorized path.
2748
  //                    $p_remove_path does not apply to 'list' mode.
2749
  //                    $p_path and $p_remove_path are commulative.
2750
  // Return Values :
2751
  //   1 on success,0 or less on error (see error code list)
2752
  // --------------------------------------------------------------------------------
2753
  function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
2754
  {
2755
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privExtractByRule", "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
2756
    $v_result=1;
2757
 
2758
    // ----- Check the path
2759
    if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../") && (substr($p_path,1,2)!=":/")))
2760
      $p_path = "./".$p_path;
2761
 
2762
    // ----- Reduce the path last (and duplicated) '/'
2763
    if (($p_path != "./") && ($p_path != "/"))
2764
    {
2765
      // ----- Look for the path end '/'
2766
      while (substr($p_path, -1) == "/")
2767
      {
2768
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
2769
        $p_path = substr($p_path, 0, strlen($p_path)-1);
2770
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
2771
      }
2772
    }
2773
 
2774
    // ----- Look for path to remove format (should end by /)
2775
    if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
2776
    {
2777
      $p_remove_path .= '/';
2778
    }
2779
    $p_remove_path_size = strlen($p_remove_path);
2780
 
2781
    // ----- Open the zip file
2782
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
2783
    if (($v_result = $this->privOpenFd('rb')) != 1)
2784
    {
2785
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2786
      return $v_result;
2787
    }
2788
 
2789
    // ----- Read the central directory informations
2790
    $v_central_dir = array();
2791
    if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
2792
    {
2793
      // ----- Close the zip file
2794
      $this->privCloseFd();
2795
 
2796
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2797
      return $v_result;
2798
    }
2799
 
2800
    // ----- Start at beginning of Central Dir
2801
    $v_pos_entry = $v_central_dir['offset'];
2802
 
2803
    // ----- Read each entry
2804
    $j_start = 0;
2805
    for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
2806
    {
2807
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry : '$i'");
2808
 
2809
      // ----- Read next Central dir entry
2810
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position before rewind : ".ftell($this->zip_fd)."'");
2811
      @rewind($this->zip_fd);
2812
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position after rewind : ".ftell($this->zip_fd)."'");
2813
      if (@fseek($this->zip_fd, $v_pos_entry))
2814
      {
2815
        // ----- Close the zip file
2816
        $this->privCloseFd();
2817
 
2818
        // ----- Error log
2819
        PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
2820
 
2821
        // ----- Return
2822
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2823
        return PclZip::errorCode();
2824
      }
2825
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position after fseek : ".ftell($this->zip_fd)."'");
2826
 
2827
      // ----- Read the file header
2828
      $v_header = array();
2829
      if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
2830
      {
2831
        // ----- Close the zip file
2832
        $this->privCloseFd();
2833
 
2834
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2835
        return $v_result;
2836
      }
2837
 
2838
      // ----- Store the index
2839
      $v_header['index'] = $i;
2840
 
2841
      // ----- Store the file position
2842
      $v_pos_entry = ftell($this->zip_fd);
2843
 
2844
      // ----- Look for the specific extract rules
2845
      $v_extract = false;
2846
 
2847
      // ----- Look for extract by name rule
2848
      if (   (isset($p_options[PCLZIP_OPT_BY_NAME]))
2849
          && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
2850
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
2851
 
2852
          // ----- Look if the filename is in the list
2853
          for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
2854
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
2855
 
2856
              // ----- Look for a directory
2857
              if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
2858
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
2859
 
2860
                  // ----- Look if the directory is in the filename path
2861
                  if (   (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
2862
                      && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
2863
                      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
2864
                      $v_extract = true;
2865
                  }
2866
              }
2867
              // ----- Look for a filename
2868
              elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
2869
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
2870
                  $v_extract = true;
2871
              }
2872
          }
2873
      }
2874
 
2875
      // ----- Look for extract by ereg rule
2876
      else if (   (isset($p_options[PCLZIP_OPT_BY_EREG]))
2877
               && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
2878
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
2879
 
2880
          if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
2881
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
2882
              $v_extract = true;
2883
          }
2884
      }
2885
 
2886
      // ----- Look for extract by preg rule
2887
      else if (   (isset($p_options[PCLZIP_OPT_BY_PREG]))
2888
               && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
2889
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
2890
 
2891
          if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
2892
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
2893
              $v_extract = true;
2894
          }
2895
      }
2896
 
2897
      // ----- Look for extract by index rule
2898
      else if (   (isset($p_options[PCLZIP_OPT_BY_INDEX]))
2899
               && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
2900
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
2901
 
2902
          // ----- Look if the index is in the list
2903
          for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
2904
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look if index '$i' is in [".$p_options[PCLZIP_OPT_BY_INDEX][$j]['start'].",".$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']."]");
2905
 
2906
              if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
2907
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
2908
                  $v_extract = true;
2909
              }
2910
              if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
2911
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
2912
                  $j_start = $j+1;
2913
              }
2914
 
2915
              if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
2916
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
2917
                  break;
2918
              }
2919
          }
2920
      }
2921
 
2922
      // ----- Look for no rule, which means extract all the archive
2923
      else {
2924
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with no rule (extract all)");
2925
          $v_extract = true;
2926
      }
2927
 
2928
	  // ----- Check compression method
2929
	  if (   ($v_extract)
2930
	      && (   ($v_header['compression'] != 8)
2931
		      && ($v_header['compression'] != 0))) {
2932
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported compression method (".$v_header['compression'].")");
2933
          $v_header['status'] = 'unsupported_compression';
2934
 
2935
          // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
2936
          if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
2937
		      && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
2938
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
2939
 
2940
              PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
2941
			                       "Filename '".$v_header['stored_filename']."' is "
2942
				  	    	  	   ."compressed by an unsupported compression "
2943
				  	    	  	   ."method (".$v_header['compression'].") ");
2944
 
2945
              //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2946
              return PclZip::errorCode();
2947
		  }
2948
	  }
2949
 
2950
	  // ----- Check encrypted files
2951
	  if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
2952
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported file encryption");
2953
          $v_header['status'] = 'unsupported_encryption';
2954
 
2955
          // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
2956
          if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
2957
		      && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
2958
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
2959
 
2960
              PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
2961
			                       "Unsupported encryption for "
2962
				  	    	  	   ." filename '".$v_header['stored_filename']
2963
								   ."'");
2964
 
2965
              //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2966
              return PclZip::errorCode();
2967
		  }
2968
	  }
2969
 
2970
      // ----- Look for real extraction
2971
      if (($v_extract) && ($v_header['status'] != 'ok')) {
2972
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "No need for extract");
2973
          $v_result = $this->privConvertHeader2FileInfo($v_header,
2974
		                                        $p_file_list[$v_nb_extracted++]);
2975
          if ($v_result != 1) {
2976
              $this->privCloseFd();
2977
              //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2978
              return $v_result;
2979
          }
2980
 
2981
          $v_extract = false;
2982
      }
2983
 
2984
      // ----- Look for real extraction
2985
      if ($v_extract)
2986
      {
2987
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file '".$v_header['filename']."', index '$i'");
2988
 
2989
        // ----- Go to the file position
2990
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
2991
        @rewind($this->zip_fd);
2992
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
2993
        if (@fseek($this->zip_fd, $v_header['offset']))
2994
        {
2995
          // ----- Close the zip file
2996
          $this->privCloseFd();
2997
 
2998
          // ----- Error log
2999
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3000
 
3001
          // ----- Return
3002
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3003
          return PclZip::errorCode();
3004
        }
3005
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
3006
 
3007
        // ----- Look for extraction as string
3008
        if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
3009
 
3010
          // ----- Extracting the file
3011
          $v_result1 = $this->privExtractFileAsString($v_header, $v_string);
3012
          if ($v_result1 < 1) {
3013
            $this->privCloseFd();
3014
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
3015
            return $v_result1;
3016
          }
3017
 
3018
          // ----- Get the only interesting attributes
3019
          if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
3020
          {
3021
            // ----- Close the zip file
3022
            $this->privCloseFd();
3023
 
3024
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3025
            return $v_result;
3026
          }
3027
 
3028
          // ----- Set the file content
3029
          $p_file_list[$v_nb_extracted]['content'] = $v_string;
3030
 
3031
          // ----- Next extracted file
3032
          $v_nb_extracted++;
3033
 
3034
          // ----- Look for user callback abort
3035
          if ($v_result1 == 2) {
3036
          	break;
3037
          }
3038
        }
3039
        // ----- Look for extraction in standard output
3040
        elseif (   (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
3041
		        && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
3042
          // ----- Extracting the file in standard output
3043
          $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
3044
          if ($v_result1 < 1) {
3045
            $this->privCloseFd();
3046
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
3047
            return $v_result1;
3048
          }
3049
 
3050
          // ----- Get the only interesting attributes
3051
          if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
3052
            $this->privCloseFd();
3053
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3054
            return $v_result;
3055
          }
3056
 
3057
          // ----- Look for user callback abort
3058
          if ($v_result1 == 2) {
3059
          	break;
3060
          }
3061
        }
3062
        // ----- Look for normal extraction
3063
        else {
3064
          // ----- Extracting the file
3065
          $v_result1 = $this->privExtractFile($v_header,
3066
		                                      $p_path, $p_remove_path,
3067
											  $p_remove_all_path,
3068
											  $p_options);
3069
          if ($v_result1 < 1) {
3070
            $this->privCloseFd();
3071
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
3072
            return $v_result1;
3073
          }
3074
 
3075
          // ----- Get the only interesting attributes
3076
          if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
3077
          {
3078
            // ----- Close the zip file
3079
            $this->privCloseFd();
3080
 
3081
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3082
            return $v_result;
3083
          }
3084
 
3085
          // ----- Look for user callback abort
3086
          if ($v_result1 == 2) {
3087
          	break;
3088
          }
3089
        }
3090
      }
3091
    }
3092
 
3093
    // ----- Close the zip file
3094
    $this->privCloseFd();
3095
 
3096
    // ----- Return
3097
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3098
    return $v_result;
3099
  }
3100
  // --------------------------------------------------------------------------------
3101
 
3102
  // --------------------------------------------------------------------------------
3103
  // Function : privExtractFile()
3104
  // Description :
3105
  // Parameters :
3106
  // Return Values :
3107
  //
3108
  // 1 : ... ?
3109
  // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
3110
  // --------------------------------------------------------------------------------
3111
  function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3112
  {
3113
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFile', "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
3114
    $v_result=1;
3115
 
3116
    // ----- Read the file header
3117
    if (($v_result = $this->privReadFileHeader($v_header)) != 1)
3118
    {
3119
      // ----- Return
3120
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3121
      return $v_result;
3122
    }
3123
 
3124
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
3125
 
3126
    // ----- Check that the file header is coherent with $p_entry info
3127
    if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
3128
        // TBC
3129
    }
3130
 
3131
    // ----- Look for all path to remove
3132
    if ($p_remove_all_path == true) {
3133
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "All path is removed");
3134
        // ----- Get the basename of the path
3135
        $p_entry['filename'] = basename($p_entry['filename']);
3136
    }
3137
 
3138
    // ----- Look for path to remove
3139
    else if ($p_remove_path != "")
3140
    {
3141
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look for some path to remove");
3142
      if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
3143
      {
3144
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The folder is the same as the removed path '".$p_entry['filename']."'");
3145
 
3146
        // ----- Change the file status
3147
        $p_entry['status'] = "filtered";
3148
 
3149
        // ----- Return
3150
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3151
        return $v_result;
3152
      }
3153
 
3154
      $p_remove_path_size = strlen($p_remove_path);
3155
      if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
3156
      {
3157
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file '".$p_entry['filename']."'");
3158
 
3159
        // ----- Remove the path
3160
        $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
3161
 
3162
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Resulting file is '".$p_entry['filename']."'");
3163
      }
3164
    }
3165
 
3166
    // ----- Add the path
3167
    if ($p_path != '')
3168
    {
3169
      $p_entry['filename'] = $p_path."/".$p_entry['filename'];
3170
    }
3171
 
3172
    // ----- Look for pre-extract callback
3173
    if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
3174
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
3175
 
3176
      // ----- Generate a local information
3177
      $v_local_header = array();
3178
      $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3179
 
3180
      // ----- Call the callback
3181
      // Here I do not use call_user_func() because I need to send a reference to the
3182
      // header.
3183
      eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
3184
      if ($v_result == 0) {
3185
        // ----- Change the file status
3186
        $p_entry['status'] = "skipped";
3187
        $v_result = 1;
3188
      }
3189
 
3190
      // ----- Look for abort result
3191
      if ($v_result == 2) {
3192
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
3193
        // ----- This status is internal and will be changed in 'skipped'
3194
        $p_entry['status'] = "aborted";
3195
      	$v_result = PCLZIP_ERR_USER_ABORTED;
3196
      }
3197
 
3198
      // ----- Update the informations
3199
      // Only some fields can be modified
3200
      $p_entry['filename'] = $v_local_header['filename'];
3201
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
3202
    }
3203
 
3204
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
3205
 
3206
    // ----- Look if extraction should be done
3207
    if ($p_entry['status'] == 'ok') {
3208
 
3209
    // ----- Look for specific actions while the file exist
3210
    if (file_exists($p_entry['filename']))
3211
    {
3212
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$p_entry['filename']."' already exists");
3213
 
3214
      // ----- Look if file is a directory
3215
      if (is_dir($p_entry['filename']))
3216
      {
3217
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is a directory");
3218
 
3219
        // ----- Change the file status
3220
        $p_entry['status'] = "already_a_directory";
3221
 
3222
        // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3223
        // For historical reason first PclZip implementation does not stop
3224
        // when this kind of error occurs.
3225
        if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3226
		    && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3227
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3228
 
3229
            PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
3230
			                     "Filename '".$p_entry['filename']."' is "
3231
								 ."already used by an existing directory");
3232
 
3233
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3234
            return PclZip::errorCode();
3235
		}
3236
      }
3237
      // ----- Look if file is write protected
3238
      else if (!is_writeable($p_entry['filename']))
3239
      {
3240
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is write protected");
3241
 
3242
        // ----- Change the file status
3243
        $p_entry['status'] = "write_protected";
3244
 
3245
        // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3246
        // For historical reason first PclZip implementation does not stop
3247
        // when this kind of error occurs.
3248
        if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3249
		    && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3250
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3251
 
3252
            PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
3253
			                     "Filename '".$p_entry['filename']."' exists "
3254
								 ."and is write protected");
3255
 
3256
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3257
            return PclZip::errorCode();
3258
		}
3259
      }
3260
 
3261
      // ----- Look if the extracted file is older
3262
      else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
3263
      {
3264
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is newer (".date("l dS of F Y h:i:s A", filemtime($p_entry['filename'])).") than the extracted file (".date("l dS of F Y h:i:s A", $p_entry['mtime']).")");
3265
        // ----- Change the file status
3266
        if (   (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
3267
		    && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
3268
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_REPLACE_NEWER is selected, file will be replaced");
3269
		}
3270
		else {
3271
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be replaced");
3272
            $p_entry['status'] = "newer_exist";
3273
 
3274
            // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3275
            // For historical reason first PclZip implementation does not stop
3276
            // when this kind of error occurs.
3277
            if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3278
		        && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3279
                //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3280
 
3281
                PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
3282
			             "Newer version of '".$p_entry['filename']."' exists "
3283
					    ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
3284
 
3285
                //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3286
                return PclZip::errorCode();
3287
		    }
3288
		}
3289
      }
3290
      else {
3291
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is older than the extrated one - will be replaced by the extracted one (".date("l dS of F Y h:i:s A", filemtime($p_entry['filename'])).") than the extracted file (".date("l dS of F Y h:i:s A", $p_entry['mtime']).")");
3292
      }
3293
    }
3294
 
3295
    // ----- Check the directory availability and create it if necessary
3296
    else {
3297
      if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
3298
        $v_dir_to_check = $p_entry['filename'];
3299
      else if (!strstr($p_entry['filename'], "/"))
3300
        $v_dir_to_check = "";
3301
      else
3302
        $v_dir_to_check = dirname($p_entry['filename']);
3303
 
3304
      if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
3305
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '".$p_entry['filename']."'");
3306
 
3307
        // ----- Change the file status
3308
        $p_entry['status'] = "path_creation_fail";
3309
 
3310
        // ----- Return
3311
        ////--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3312
        //return $v_result;
3313
        $v_result = 1;
3314
      }
3315
    }
3316
    }
3317
 
3318
    // ----- Look if extraction should be done
3319
    if ($p_entry['status'] == 'ok') {
3320
 
3321
      // ----- Do the extraction (if not a folder)
3322
      if (!(($p_entry['external']&0x00000010)==0x00000010))
3323
      {
3324
        // ----- Look for not compressed file
3325
        if ($p_entry['compression'] == 0) {
3326
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
3327
 
3328
		  // ----- Opening destination file
3329
          if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
3330
          {
3331
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
3332
 
3333
            // ----- Change the file status
3334
            $p_entry['status'] = "write_error";
3335
 
3336
            // ----- Return
3337
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3338
            return $v_result;
3339
          }
3340
 
3341
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read '".$p_entry['size']."' bytes");
3342
 
3343
          // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3344
          $v_size = $p_entry['compressed_size'];
3345
          while ($v_size != 0)
3346
          {
3347
            $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3348
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read $v_read_size bytes");
3349
            $v_buffer = fread($this->zip_fd, $v_read_size);
3350
            $v_binary_data = pack('a'.$v_read_size, $v_buffer);
3351
            @fwrite($v_dest_file, $v_binary_data, $v_read_size);
3352
            $v_size -= $v_read_size;
3353
          }
3354
 
3355
          // ----- Closing the destination file
3356
          fclose($v_dest_file);
3357
 
3358
          // ----- Change the file mtime
3359
          touch($p_entry['filename'], $p_entry['mtime']);
3360
 
3361
 
3362
        }
3363
        else {
3364
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file (Compression method ".$p_entry['compression'].")");
3365
          // ----- TBC
3366
          // Need to be finished
3367
          if (($p_entry['flag'] & 1) == 1) {
3368
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File is encrypted");
3369
            /*
3370
              // ----- Read the encryption header
3371
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read 12 encryption header bytes");
3372
              $v_encryption_header = @fread($this->zip_fd, 12);
3373
 
3374
              // ----- Read the encrypted & compressed file in a buffer
3375
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read '".($p_entry['compressed_size']-12)."' compressed & encrypted bytes");
3376
              $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']-12);
3377
 
3378
              // ----- Decrypt the buffer
3379
              $this->privDecrypt($v_encryption_header, $v_buffer,
3380
			                     $p_entry['compressed_size']-12, $p_entry['crc']);
3381
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Buffer is '".$v_buffer."'");
3382
              */
3383
          }
3384
          else {
3385
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read '".$p_entry['compressed_size']."' compressed bytes");
3386
              // ----- Read the compressed file in a buffer (one shot)
3387
              $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3388
          }
3389
 
3390
          // ----- Decompress the file
3391
          $v_file_content = @gzinflate($v_buffer);
3392
          unset($v_buffer);
3393
          if ($v_file_content === FALSE) {
3394
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to inflate compressed file");
3395
 
3396
            // ----- Change the file status
3397
            // TBC
3398
            $p_entry['status'] = "error";
3399
 
3400
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3401
            return $v_result;
3402
          }
3403
 
3404
          // ----- Opening destination file
3405
          if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
3406
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
3407
 
3408
            // ----- Change the file status
3409
            $p_entry['status'] = "write_error";
3410
 
3411
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3412
            return $v_result;
3413
          }
3414
 
3415
          // ----- Write the uncompressed data
3416
          @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
3417
          unset($v_file_content);
3418
 
3419
          // ----- Closing the destination file
3420
          @fclose($v_dest_file);
3421
 
3422
          // ----- Change the file mtime
3423
          @touch($p_entry['filename'], $p_entry['mtime']);
3424
        }
3425
 
3426
        // ----- Look for chmod option
3427
        if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
3428
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "chmod option activated '".$p_options[PCLZIP_OPT_SET_CHMOD]."'");
3429
 
3430
          // ----- Change the mode of the file
3431
          @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
3432
        }
3433
 
3434
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
3435
      }
3436
    }
3437
 
3438
	// ----- Change abort status
3439
	if ($p_entry['status'] == "aborted") {
3440
      $p_entry['status'] = "skipped";
3441
	}
3442
 
3443
    // ----- Look for post-extract callback
3444
    elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
3445
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
3446
 
3447
      // ----- Generate a local information
3448
      $v_local_header = array();
3449
      $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3450
 
3451
      // ----- Call the callback
3452
      // Here I do not use call_user_func() because I need to send a reference to the
3453
      // header.
3454
      eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
3455
 
3456
      // ----- Look for abort result
3457
      if ($v_result == 2) {
3458
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
3459
      	$v_result = PCLZIP_ERR_USER_ABORTED;
3460
      }
3461
    }
3462
 
3463
    // ----- Return
3464
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3465
    return $v_result;
3466
  }
3467
  // --------------------------------------------------------------------------------
3468
 
3469
  // --------------------------------------------------------------------------------
3470
  // Function : privExtractFileInOutput()
3471
  // Description :
3472
  // Parameters :
3473
  // Return Values :
3474
  // --------------------------------------------------------------------------------
3475
  function privExtractFileInOutput(&$p_entry, &$p_options)
3476
  {
3477
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileInOutput', "");
3478
    $v_result=1;
3479
 
3480
    // ----- Read the file header
3481
    if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
3482
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3483
      return $v_result;
3484
    }
3485
 
3486
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
3487
 
3488
    // ----- Check that the file header is coherent with $p_entry info
3489
    if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
3490
        // TBC
3491
    }
3492
 
3493
    // ----- Look for pre-extract callback
3494
    if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
3495
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
3496
 
3497
      // ----- Generate a local information
3498
      $v_local_header = array();
3499
      $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3500
 
3501
      // ----- Call the callback
3502
      // Here I do not use call_user_func() because I need to send a reference to the
3503
      // header.
3504
      eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
3505
      if ($v_result == 0) {
3506
        // ----- Change the file status
3507
        $p_entry['status'] = "skipped";
3508
        $v_result = 1;
3509
      }
3510
 
3511
      // ----- Look for abort result
3512
      if ($v_result == 2) {
3513
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
3514
        // ----- This status is internal and will be changed in 'skipped'
3515
        $p_entry['status'] = "aborted";
3516
      	$v_result = PCLZIP_ERR_USER_ABORTED;
3517
      }
3518
 
3519
      // ----- Update the informations
3520
      // Only some fields can be modified
3521
      $p_entry['filename'] = $v_local_header['filename'];
3522
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
3523
    }
3524
 
3525
    // ----- Trace
3526
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
3527
 
3528
    // ----- Look if extraction should be done
3529
    if ($p_entry['status'] == 'ok') {
3530
 
3531
      // ----- Do the extraction (if not a folder)
3532
      if (!(($p_entry['external']&0x00000010)==0x00000010)) {
3533
        // ----- Look for not compressed file
3534
        if ($p_entry['compressed_size'] == $p_entry['size']) {
3535
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
3536
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
3537
 
3538
          // ----- Read the file in a buffer (one shot)
3539
          $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3540
 
3541
          // ----- Send the file to the output
3542
          echo $v_buffer;
3543
          unset($v_buffer);
3544
        }
3545
        else {
3546
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file");
3547
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Reading '".$p_entry['size']."' bytes");
3548
 
3549
          // ----- Read the compressed file in a buffer (one shot)
3550
          $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3551
 
3552
          // ----- Decompress the file
3553
          $v_file_content = gzinflate($v_buffer);
3554
          unset($v_buffer);
3555
 
3556
          // ----- Send the file to the output
3557
          echo $v_file_content;
3558
          unset($v_file_content);
3559
        }
3560
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
3561
      }
3562
    }
3563
 
3564
	// ----- Change abort status
3565
	if ($p_entry['status'] == "aborted") {
3566
      $p_entry['status'] = "skipped";
3567
	}
3568
 
3569
    // ----- Look for post-extract callback
3570
    elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
3571
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
3572
 
3573
      // ----- Generate a local information
3574
      $v_local_header = array();
3575
      $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3576
 
3577
      // ----- Call the callback
3578
      // Here I do not use call_user_func() because I need to send a reference to the
3579
      // header.
3580
      eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
3581
 
3582
      // ----- Look for abort result
3583
      if ($v_result == 2) {
3584
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
3585
      	$v_result = PCLZIP_ERR_USER_ABORTED;
3586
      }
3587
    }
3588
 
3589
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3590
    return $v_result;
3591
  }
3592
  // --------------------------------------------------------------------------------
3593
 
3594
  // --------------------------------------------------------------------------------
3595
  // Function : privExtractFileAsString()
3596
  // Description :
3597
  // Parameters :
3598
  // Return Values :
3599
  // --------------------------------------------------------------------------------
3600
  function privExtractFileAsString(&$p_entry, &$p_string)
3601
  {
3602
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileAsString', "p_entry['filename']='".$p_entry['filename']."'");
3603
    $v_result=1;
3604
 
3605
    // ----- Read the file header
3606
    $v_header = array();
3607
    if (($v_result = $this->privReadFileHeader($v_header)) != 1)
3608
    {
3609
      // ----- Return
3610
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3611
      return $v_result;
3612
    }
3613
 
3614
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
3615
 
3616
    // ----- Check that the file header is coherent with $p_entry info
3617
    if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
3618
        // TBC
3619
    }
3620
 
3621
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file in string (with path) '".$p_entry['filename']."', size '$v_header[size]'");
3622
 
3623
    // ----- Do the extraction (if not a folder)
3624
    if (!(($p_entry['external']&0x00000010)==0x00000010))
3625
    {
3626
      // ----- Look for not compressed file
3627
      if ($p_entry['compressed_size'] == $p_entry['size'])
3628
      {
3629
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
3630
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
3631
 
3632
        // ----- Reading the file
3633
        $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
3634
      }
3635
      else
3636
      {
3637
        // ----- Trace
3638
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file");
3639
 
3640
        // ----- Reading the file
3641
        $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
3642
 
3643
        // ----- Decompress the file
3644
        if (($p_string = @gzinflate($v_data)) === FALSE) {
3645
            // TBC
3646
        }
3647
      }
3648
 
3649
      // ----- Trace
3650
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
3651
    }
3652
    else {
3653
        // TBC : error : can not extract a folder in a string
3654
    }
3655
 
3656
    // ----- Return
3657
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3658
    return $v_result;
3659
  }
3660
  // --------------------------------------------------------------------------------
3661
 
3662
  // --------------------------------------------------------------------------------
3663
  // Function : privReadFileHeader()
3664
  // Description :
3665
  // Parameters :
3666
  // Return Values :
3667
  // --------------------------------------------------------------------------------
3668
  function privReadFileHeader(&$p_header)
3669
  {
3670
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadFileHeader", "");
3671
    $v_result=1;
3672
 
3673
    // ----- Read the 4 bytes signature
3674
    $v_binary_data = @fread($this->zip_fd, 4);
3675
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
3676
    $v_data = unpack('Vid', $v_binary_data);
3677
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
3678
 
3679
    // ----- Check signature
3680
    if ($v_data['id'] != 0x04034b50)
3681
    {
3682
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid File header");
3683
 
3684
      // ----- Error log
3685
      PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
3686
 
3687
      // ----- Return
3688
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3689
      return PclZip::errorCode();
3690
    }
3691
 
3692
    // ----- Read the first 42 bytes of the header
3693
    $v_binary_data = fread($this->zip_fd, 26);
3694
 
3695
    // ----- Look for invalid block size
3696
    if (strlen($v_binary_data) != 26)
3697
    {
3698
      $p_header['filename'] = "";
3699
      $p_header['status'] = "invalid_header";
3700
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
3701
 
3702
      // ----- Error log
3703
      PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
3704
 
3705
      // ----- Return
3706
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3707
      return PclZip::errorCode();
3708
    }
3709
 
3710
    // ----- Extract the values
3711
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header : '".$v_binary_data."'");
3712
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header (Hex) : '".bin2hex($v_binary_data)."'");
3713
    $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
3714
 
3715
    // ----- Get filename
3716
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "File name length : ".$v_data['filename_len']);
3717
    $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
3718
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename : \''.$p_header['filename'].'\'');
3719
 
3720
    // ----- Get extra_fields
3721
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extra field length : ".$v_data['extra_len']);
3722
    if ($v_data['extra_len'] != 0) {
3723
      $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
3724
    }
3725
    else {
3726
      $p_header['extra'] = '';
3727
    }
3728
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Extra field : \''.bin2hex($p_header['extra']).'\'');
3729
 
3730
    // ----- Extract properties
3731
    $p_header['version_extracted'] = $v_data['version'];
3732
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : ('.$p_header['version_extracted'].') \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
3733
    $p_header['compression'] = $v_data['compression'];
3734
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compression method : \''.$p_header['compression'].'\'');
3735
    $p_header['size'] = $v_data['size'];
3736
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_header['size'].'\'');
3737
    $p_header['compressed_size'] = $v_data['compressed_size'];
3738
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
3739
    $p_header['crc'] = $v_data['crc'];
3740
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
3741
    $p_header['flag'] = $v_data['flag'];
3742
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag : \''.$p_header['flag'].'\'');
3743
 
3744
    // ----- Recuperate date in UNIX format
3745
    $p_header['mdate'] = $v_data['mdate'];
3746
    $p_header['mtime'] = $v_data['mtime'];
3747
    if ($p_header['mdate'] && $p_header['mtime'])
3748
    {
3749
      // ----- Extract time
3750
      $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
3751
      $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
3752
      $v_seconde = ($p_header['mtime'] & 0x001F)*2;
3753
 
3754
      // ----- Extract date
3755
      $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
3756
      $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
3757
      $v_day = $p_header['mdate'] & 0x001F;
3758
 
3759
      // ----- Get UNIX date format
3760
      $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
3761
 
3762
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
3763
    }
3764
    else
3765
    {
3766
      $p_header['mtime'] = time();
3767
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
3768
    }
3769
 
3770
    // TBC
3771
    //for(reset($v_data); $key = key($v_data); next($v_data)) {
3772
    //  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Attribut[$key] = ".$v_data[$key]);
3773
    //}
3774
 
3775
    // ----- Set the stored filename
3776
    $p_header['stored_filename'] = $p_header['filename'];
3777
 
3778
    // ----- Set the status field
3779
    $p_header['status'] = "ok";
3780
 
3781
    // ----- Return
3782
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3783
    return $v_result;
3784
  }
3785
  // --------------------------------------------------------------------------------
3786
 
3787
  // --------------------------------------------------------------------------------
3788
  // Function : privReadCentralFileHeader()
3789
  // Description :
3790
  // Parameters :
3791
  // Return Values :
3792
  // --------------------------------------------------------------------------------
3793
  function privReadCentralFileHeader(&$p_header)
3794
  {
3795
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadCentralFileHeader", "");
3796
    $v_result=1;
3797
 
3798
    // ----- Read the 4 bytes signature
3799
    $v_binary_data = @fread($this->zip_fd, 4);
3800
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
3801
    $v_data = unpack('Vid', $v_binary_data);
3802
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
3803
 
3804
    // ----- Check signature
3805
    if ($v_data['id'] != 0x02014b50)
3806
    {
3807
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid Central Dir File signature");
3808
 
3809
      // ----- Error log
3810
      PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
3811
 
3812
      // ----- Return
3813
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3814
      return PclZip::errorCode();
3815
    }
3816
 
3817
    // ----- Read the first 42 bytes of the header
3818
    $v_binary_data = fread($this->zip_fd, 42);
3819
 
3820
    // ----- Look for invalid block size
3821
    if (strlen($v_binary_data) != 42)
3822
    {
3823
      $p_header['filename'] = "";
3824
      $p_header['status'] = "invalid_header";
3825
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
3826
 
3827
      // ----- Error log
3828
      PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
3829
 
3830
      // ----- Return
3831
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3832
      return PclZip::errorCode();
3833
    }
3834
 
3835
    // ----- Extract the values
3836
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header : '".$v_binary_data."'");
3837
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header (Hex) : '".bin2hex($v_binary_data)."'");
3838
    $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);
3839
 
3840
    // ----- Get filename
3841
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File name length : ".$p_header['filename_len']);
3842
    if ($p_header['filename_len'] != 0)
3843
      $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
3844
    else
3845
      $p_header['filename'] = '';
3846
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Filename : \''.$p_header['filename'].'\'');
3847
 
3848
    // ----- Get extra
3849
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Extra length : ".$p_header['extra_len']);
3850
    if ($p_header['extra_len'] != 0)
3851
      $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
3852
    else
3853
      $p_header['extra'] = '';
3854
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Extra : \''.$p_header['extra'].'\'');
3855
 
3856
    // ----- Get comment
3857
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Comment length : ".$p_header['comment_len']);
3858
    if ($p_header['comment_len'] != 0)
3859
      $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
3860
    else
3861
      $p_header['comment'] = '';
3862
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Comment : \''.$p_header['comment'].'\'');
3863
 
3864
    // ----- Extract properties
3865
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version : \''.($p_header['version']/10).'.'.($p_header['version']%10).'\'');
3866
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
3867
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Size : \''.$p_header['size'].'\'');
3868
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
3869
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
3870
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Flag : \''.$p_header['flag'].'\'');
3871
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Offset : \''.$p_header['offset'].'\'');
3872
 
3873
    // ----- Recuperate date in UNIX format
3874
    if ($p_header['mdate'] && $p_header['mtime'])
3875
    {
3876
      // ----- Extract time
3877
      $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
3878
      $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
3879
      $v_seconde = ($p_header['mtime'] & 0x001F)*2;
3880
 
3881
      // ----- Extract date
3882
      $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
3883
      $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
3884
      $v_day = $p_header['mdate'] & 0x001F;
3885
 
3886
      // ----- Get UNIX date format
3887
      $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
3888
 
3889
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
3890
    }
3891
    else
3892
    {
3893
      $p_header['mtime'] = time();
3894
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
3895
    }
3896
 
3897
    // ----- Set the stored filename
3898
    $p_header['stored_filename'] = $p_header['filename'];
3899
 
3900
    // ----- Set default status to ok
3901
    $p_header['status'] = 'ok';
3902
 
3903
    // ----- Look if it is a directory
3904
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Internal (Hex) : '".sprintf("Ox%04X", $p_header['internal'])."'");
3905
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "External (Hex) : '".sprintf("Ox%04X", $p_header['external'])."' (".(($p_header['external']&0x00000010)==0x00000010?'is a folder':'is a file').')');
3906
    if (substr($p_header['filename'], -1) == '/') {
3907
      //$p_header['external'] = 0x41FF0010;
3908
      $p_header['external'] = 0x00000010;
3909
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Force folder external : \''.sprintf("Ox%04X", $p_header['external']).'\'');
3910
    }
3911
 
3912
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Header of filename : \''.$p_header['filename'].'\'');
3913
 
3914
    // ----- Return
3915
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3916
    return $v_result;
3917
  }
3918
  // --------------------------------------------------------------------------------
3919
 
3920
  // --------------------------------------------------------------------------------
3921
  // Function : privCheckFileHeaders()
3922
  // Description :
3923
  // Parameters :
3924
  // Return Values :
3925
  //   1 on success,
3926
  //   0 on error;
3927
  // --------------------------------------------------------------------------------
3928
  function privCheckFileHeaders(&$p_local_header, &$p_central_header)
3929
  {
3930
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFileHeaders", "");
3931
    $v_result=1;
3932
 
3933
	// ----- Check the static values
3934
	// TBC
3935
	if ($p_local_header['filename'] != $p_central_header['filename']) {
3936
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename" : TBC To Be Completed');
3937
	}
3938
	if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
3939
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "version_extracted" : TBC To Be Completed');
3940
	}
3941
	if ($p_local_header['flag'] != $p_central_header['flag']) {
3942
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "flag" : TBC To Be Completed');
3943
	}
3944
	if ($p_local_header['compression'] != $p_central_header['compression']) {
3945
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "compression" : TBC To Be Completed');
3946
	}
3947
	if ($p_local_header['mtime'] != $p_central_header['mtime']) {
3948
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "mtime" : TBC To Be Completed');
3949
	}
3950
	if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
3951
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename_len" : TBC To Be Completed');
3952
	}
3953
 
3954
	// ----- Look for flag bit 3
3955
	if (($p_local_header['flag'] & 8) == 8) {
3956
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Purpose bit flag bit 3 set !');
3957
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'File size, compression size and crc found in central header');
3958
        $p_local_header['size'] = $p_central_header['size'];
3959
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_local_header['size'].'\'');
3960
        $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
3961
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_local_header['compressed_size'].'\'');
3962
        $p_local_header['crc'] = $p_central_header['crc'];
3963
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_local_header['crc']).'\'');
3964
	}
3965
 
3966
    // ----- Return
3967
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3968
    return $v_result;
3969
  }
3970
  // --------------------------------------------------------------------------------
3971
 
3972
  // --------------------------------------------------------------------------------
3973
  // Function : privReadEndCentralDir()
3974
  // Description :
3975
  // Parameters :
3976
  // Return Values :
3977
  // --------------------------------------------------------------------------------
3978
  function privReadEndCentralDir(&$p_central_dir)
3979
  {
3980
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadEndCentralDir", "");
3981
    $v_result=1;
3982
 
3983
    // ----- Go to the end of the zip file
3984
    $v_size = filesize($this->zipname);
3985
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size of the file :$v_size");
3986
    @fseek($this->zip_fd, $v_size);
3987
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position at end of zip file : \''.ftell($this->zip_fd).'\'');
3988
    if (@ftell($this->zip_fd) != $v_size)
3989
    {
3990
      // ----- Error log
3991
      PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
3992
 
3993
      // ----- Return
3994
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3995
      return PclZip::errorCode();
3996
    }
3997
 
3998
    // ----- First try : look if this is an archive with no commentaries (most of the time)
3999
    // in this case the end of central dir is at 22 bytes of the file end
4000
    $v_found = 0;
4001
    if ($v_size > 26) {
4002
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Look for central dir with no comment');
4003
      @fseek($this->zip_fd, $v_size-22);
4004
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after min central position : \''.ftell($this->zip_fd).'\'');
4005
      if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
4006
      {
4007
        // ----- Error log
4008
        PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
4009
 
4010
        // ----- Return
4011
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4012
        return PclZip::errorCode();
4013
      }
4014
 
4015
      // ----- Read for bytes
4016
      $v_binary_data = @fread($this->zip_fd, 4);
4017
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
4018
      $v_data = @unpack('Vid', $v_binary_data);
4019
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
4020
 
4021
      // ----- Check signature
4022
      if ($v_data['id'] == 0x06054b50) {
4023
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found central dir at the default position.");
4024
        $v_found = 1;
4025
      }
4026
 
4027
      $v_pos = ftell($this->zip_fd);
4028
    }
4029
 
4030
    // ----- Go back to the maximum possible size of the Central Dir End Record
4031
    if (!$v_found) {
4032
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Start extended search of end central dir');
4033
      $v_maximum_size = 65557; // 0xFFFF + 22;
4034
      if ($v_maximum_size > $v_size)
4035
        $v_maximum_size = $v_size;
4036
      @fseek($this->zip_fd, $v_size-$v_maximum_size);
4037
      if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
4038
      {
4039
        // ----- Error log
4040
        PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
4041
 
4042
        // ----- Return
4043
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4044
        return PclZip::errorCode();
4045
      }
4046
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after max central position : \''.ftell($this->zip_fd).'\'');
4047
 
4048
      // ----- Read byte per byte in order to find the signature
4049
      $v_pos = ftell($this->zip_fd);
4050
      $v_bytes = 0x00000000;
4051
      while ($v_pos < $v_size)
4052
      {
4053
        // ----- Read a byte
4054
        $v_byte = @fread($this->zip_fd, 1);
4055
 
4056
        // -----  Add the byte
4057
        $v_bytes = ($v_bytes << 8) | Ord($v_byte);
4058
 
4059
        // ----- Compare the bytes
4060
        if ($v_bytes == 0x504b0506)
4061
        {
4062
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Found End Central Dir signature at position : \''.ftell($this->zip_fd).'\'');
4063
          $v_pos++;
4064
          break;
4065
        }
4066
 
4067
        $v_pos++;
4068
      }
4069
 
4070
      // ----- Look if not found end of central dir
4071
      if ($v_pos == $v_size)
4072
      {
4073
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to find End of Central Dir Record signature");
4074
 
4075
        // ----- Error log
4076
        PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
4077
 
4078
        // ----- Return
4079
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4080
        return PclZip::errorCode();
4081
      }
4082
    }
4083
 
4084
    // ----- Read the first 18 bytes of the header
4085
    $v_binary_data = fread($this->zip_fd, 18);
4086
 
4087
    // ----- Look for invalid block size
4088
    if (strlen($v_binary_data) != 18)
4089
    {
4090
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
4091
 
4092
      // ----- Error log
4093
      PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
4094
 
4095
      // ----- Return
4096
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4097
      return PclZip::errorCode();
4098
    }
4099
 
4100
    // ----- Extract the values
4101
    ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record : '".$v_binary_data."'");
4102
    ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record (Hex) : '".bin2hex($v_binary_data)."'");
4103
    $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
4104
 
4105
    // ----- Check the global size
4106
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Comment length : ".$v_data['comment_size']);
4107
    if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
4108
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The central dir is not at the end of the archive. Some trailing bytes exists after the archive.");
4109
 
4110
	  // ----- Removed in release 2.2 see readme file
4111
	  // The check of the file size is a little too strict.
4112
	  // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
4113
	  // While decrypted, zip has training 0 bytes
4114
	  if (0) {
4115
      // ----- Error log
4116
      PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
4117
	                       'The central dir is not at the end of the archive.'
4118
						   .' Some trailing bytes exists after the archive.');
4119
 
4120
      // ----- Return
4121
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4122
      return PclZip::errorCode();
4123
	  }
4124
    }
4125
 
4126
    // ----- Get comment
4127
    if ($v_data['comment_size'] != 0)
4128
      $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
4129
    else
4130
      $p_central_dir['comment'] = '';
4131
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment : \''.$p_central_dir['comment'].'\'');
4132
 
4133
    $p_central_dir['entries'] = $v_data['entries'];
4134
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries : \''.$p_central_dir['entries'].'\'');
4135
    $p_central_dir['disk_entries'] = $v_data['disk_entries'];
4136
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries for this disk : \''.$p_central_dir['disk_entries'].'\'');
4137
    $p_central_dir['offset'] = $v_data['offset'];
4138
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Offset of Central Dir : \''.$p_central_dir['offset'].'\'');
4139
    $p_central_dir['size'] = $v_data['size'];
4140
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size of Central Dir : \''.$p_central_dir['size'].'\'');
4141
    $p_central_dir['disk'] = $v_data['disk'];
4142
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Disk number : \''.$p_central_dir['disk'].'\'');
4143
    $p_central_dir['disk_start'] = $v_data['disk_start'];
4144
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Start disk number : \''.$p_central_dir['disk_start'].'\'');
4145
 
4146
    // TBC
4147
    //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
4148
    //  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "central_dir[$key] = ".$p_central_dir[$key]);
4149
    //}
4150
 
4151
    // ----- Return
4152
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4153
    return $v_result;
4154
  }
4155
  // --------------------------------------------------------------------------------
4156
 
4157
  // --------------------------------------------------------------------------------
4158
  // Function : privDeleteByRule()
4159
  // Description :
4160
  // Parameters :
4161
  // Return Values :
4162
  // --------------------------------------------------------------------------------
4163
  function privDeleteByRule(&$p_result_list, &$p_options)
4164
  {
4165
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDeleteByRule", "");
4166
    $v_result=1;
4167
    $v_list_detail = array();
4168
 
4169
    // ----- Open the zip file
4170
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
4171
    if (($v_result=$this->privOpenFd('rb')) != 1)
4172
    {
4173
      // ----- Return
4174
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4175
      return $v_result;
4176
    }
4177
 
4178
    // ----- Read the central directory informations
4179
    $v_central_dir = array();
4180
    if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
4181
    {
4182
      $this->privCloseFd();
4183
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4184
      return $v_result;
4185
    }
4186
 
4187
    // ----- Go to beginning of File
4188
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
4189
    @rewind($this->zip_fd);
4190
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
4191
 
4192
    // ----- Scan all the files
4193
    // ----- Start at beginning of Central Dir
4194
    $v_pos_entry = $v_central_dir['offset'];
4195
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
4196
    @rewind($this->zip_fd);
4197
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
4198
    if (@fseek($this->zip_fd, $v_pos_entry))
4199
    {
4200
      // ----- Close the zip file
4201
      $this->privCloseFd();
4202
 
4203
      // ----- Error log
4204
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
4205
 
4206
      // ----- Return
4207
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4208
      return PclZip::errorCode();
4209
    }
4210
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
4211
 
4212
    // ----- Read each entry
4213
    $v_header_list = array();
4214
    $j_start = 0;
4215
    for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
4216
    {
4217
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry (index '$i')");
4218
 
4219
      // ----- Read the file header
4220
      $v_header_list[$v_nb_extracted] = array();
4221
      if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
4222
      {
4223
        // ----- Close the zip file
4224
        $this->privCloseFd();
4225
 
4226
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4227
        return $v_result;
4228
      }
4229
 
4230
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename (index '$i') : '".$v_header_list[$v_nb_extracted]['stored_filename']."'");
4231
 
4232
      // ----- Store the index
4233
      $v_header_list[$v_nb_extracted]['index'] = $i;
4234
 
4235
      // ----- Look for the specific extract rules
4236
      $v_found = false;
4237
 
4238
      // ----- Look for extract by name rule
4239
      if (   (isset($p_options[PCLZIP_OPT_BY_NAME]))
4240
          && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
4241
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
4242
 
4243
          // ----- Look if the filename is in the list
4244
          for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
4245
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
4246
 
4247
              // ----- Look for a directory
4248
              if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
4249
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
4250
 
4251
                  // ----- Look if the directory is in the filename path
4252
                  if (   (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
4253
                      && (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])) {
4254
                      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
4255
                      $v_found = true;
4256
                  }
4257
                  elseif (   (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
4258
                          && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
4259
                      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The entry is the searched directory");
4260
                      $v_found = true;
4261
                  }
4262
              }
4263
              // ----- Look for a filename
4264
              elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
4265
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
4266
                  $v_found = true;
4267
              }
4268
          }
4269
      }
4270
 
4271
      // ----- Look for extract by ereg rule
4272
      else if (   (isset($p_options[PCLZIP_OPT_BY_EREG]))
4273
               && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
4274
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
4275
 
4276
          if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
4277
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
4278
              $v_found = true;
4279
          }
4280
      }
4281
 
4282
      // ----- Look for extract by preg rule
4283
      else if (   (isset($p_options[PCLZIP_OPT_BY_PREG]))
4284
               && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
4285
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
4286
 
4287
          if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
4288
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
4289
              $v_found = true;
4290
          }
4291
      }
4292
 
4293
      // ----- Look for extract by index rule
4294
      else if (   (isset($p_options[PCLZIP_OPT_BY_INDEX]))
4295
               && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
4296
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
4297
 
4298
          // ----- Look if the index is in the list
4299
          for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
4300
              //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look if index '$i' is in [".$p_options[PCLZIP_OPT_BY_INDEX][$j]['start'].",".$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']."]");
4301
 
4302
              if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
4303
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
4304
                  $v_found = true;
4305
              }
4306
              if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
4307
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
4308
                  $j_start = $j+1;
4309
              }
4310
 
4311
              if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
4312
                  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
4313
                  break;
4314
              }
4315
          }
4316
      }
4317
 
4318
      // ----- Look for deletion
4319
      if ($v_found)
4320
      {
4321
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' need to be deleted");
4322
        unset($v_header_list[$v_nb_extracted]);
4323
      }
4324
      else
4325
      {
4326
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' will not be deleted");
4327
        $v_nb_extracted++;
4328
      }
4329
    }
4330
 
4331
    // ----- Look if something need to be deleted
4332
    if ($v_nb_extracted > 0) {
4333
 
4334
        // ----- Creates a temporay file
4335
        $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
4336
 
4337
        // ----- Creates a temporary zip archive
4338
        $v_temp_zip = new PclZip($v_zip_temp_name);
4339
 
4340
        // ----- Open the temporary zip file in write mode
4341
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary write mode");
4342
        if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
4343
            $this->privCloseFd();
4344
 
4345
            // ----- Return
4346
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4347
            return $v_result;
4348
        }
4349
 
4350
        // ----- Look which file need to be kept
4351
        for ($i=0; $i<sizeof($v_header_list); $i++) {
4352
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Keep entry index '$i' : '".$v_header_list[$i]['filename']."'");
4353
 
4354
            // ----- Calculate the position of the header
4355
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset='". $v_header_list[$i]['offset']."'");
4356
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
4357
            @rewind($this->zip_fd);
4358
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
4359
            if (@fseek($this->zip_fd,  $v_header_list[$i]['offset'])) {
4360
                // ----- Close the zip file
4361
                $this->privCloseFd();
4362
                $v_temp_zip->privCloseFd();
4363
                @unlink($v_zip_temp_name);
4364
 
4365
                // ----- Error log
4366
                PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
4367
 
4368
                // ----- Return
4369
                //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4370
                return PclZip::errorCode();
4371
            }
4372
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
4373
 
4374
            // ----- Read the file header
4375
            $v_local_header = array();
4376
            if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
4377
                // ----- Close the zip file
4378
                $this->privCloseFd();
4379
                $v_temp_zip->privCloseFd();
4380
                @unlink($v_zip_temp_name);
4381
 
4382
                // ----- Return
4383
                //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4384
                return $v_result;
4385
            }
4386
 
4387
            // ----- Check that local file header is same as central file header
4388
            if ($this->privCheckFileHeaders($v_local_header,
4389
			                                $v_header_list[$i]) != 1) {
4390
                // TBC
4391
            }
4392
            unset($v_local_header);
4393
 
4394
            // ----- Write the file header
4395
            if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
4396
                // ----- Close the zip file
4397
                $this->privCloseFd();
4398
                $v_temp_zip->privCloseFd();
4399
                @unlink($v_zip_temp_name);
4400
 
4401
                // ----- Return
4402
                //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4403
                return $v_result;
4404
            }
4405
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset for this file is '".$v_header_list[$i]['offset']."'");
4406
 
4407
            // ----- Read/write the data block
4408
            if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
4409
                // ----- Close the zip file
4410
                $this->privCloseFd();
4411
                $v_temp_zip->privCloseFd();
4412
                @unlink($v_zip_temp_name);
4413
 
4414
                // ----- Return
4415
                //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4416
                return $v_result;
4417
            }
4418
        }
4419
 
4420
        // ----- Store the offset of the central dir
4421
        $v_offset = @ftell($v_temp_zip->zip_fd);
4422
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "New offset of central dir : $v_offset");
4423
 
4424
        // ----- Re-Create the Central Dir files header
4425
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the new central directory");
4426
        for ($i=0; $i<sizeof($v_header_list); $i++) {
4427
            // ----- Create the file header
4428
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset of file : ".$v_header_list[$i]['offset']);
4429
            if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
4430
                $v_temp_zip->privCloseFd();
4431
                $this->privCloseFd();
4432
                @unlink($v_zip_temp_name);
4433
 
4434
                // ----- Return
4435
                //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4436
                return $v_result;
4437
            }
4438
 
4439
            // ----- Transform the header to a 'usable' info
4440
            $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
4441
        }
4442
 
4443
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the central directory footer");
4444
 
4445
        // ----- Zip file comment
4446
        $v_comment = '';
4447
        if (isset($p_options[PCLZIP_OPT_COMMENT])) {
4448
          $v_comment = $p_options[PCLZIP_OPT_COMMENT];
4449
        }
4450
 
4451
        // ----- Calculate the size of the central header
4452
        $v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
4453
 
4454
        // ----- Create the central dir footer
4455
        if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
4456
            // ----- Reset the file list
4457
            unset($v_header_list);
4458
            $v_temp_zip->privCloseFd();
4459
            $this->privCloseFd();
4460
            @unlink($v_zip_temp_name);
4461
 
4462
            // ----- Return
4463
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4464
            return $v_result;
4465
        }
4466
 
4467
        // ----- Close
4468
        $v_temp_zip->privCloseFd();
4469
        $this->privCloseFd();
4470
 
4471
        // ----- Delete the zip file
4472
        // TBC : I should test the result ...
4473
        @unlink($this->zipname);
4474
 
4475
        // ----- Rename the temporary file
4476
        // TBC : I should test the result ...
4477
        //@rename($v_zip_temp_name, $this->zipname);
4478
        PclZipUtilRename($v_zip_temp_name, $this->zipname);
4479
 
4480
        // ----- Destroy the temporary archive
4481
        unset($v_temp_zip);
4482
    }
4483
 
4484
    // ----- Remove every files : reset the file
4485
    else if ($v_central_dir['entries'] != 0) {
4486
        $this->privCloseFd();
4487
 
4488
        if (($v_result = $this->privOpenFd('wb')) != 1) {
4489
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4490
          return $v_result;
4491
        }
4492
 
4493
        if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
4494
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4495
          return $v_result;
4496
        }
4497
 
4498
        $this->privCloseFd();
4499
    }
4500
 
4501
    // ----- Return
4502
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4503
    return $v_result;
4504
  }
4505
  // --------------------------------------------------------------------------------
4506
 
4507
  // --------------------------------------------------------------------------------
4508
  // Function : privDirCheck()
4509
  // Description :
4510
  //   Check if a directory exists, if not it creates it and all the parents directory
4511
  //   which may be useful.
4512
  // Parameters :
4513
  //   $p_dir : Directory path to check.
4514
  // Return Values :
4515
  //    1 : OK
4516
  //   -1 : Unable to create directory
4517
  // --------------------------------------------------------------------------------
4518
  function privDirCheck($p_dir, $p_is_dir=false)
4519
  {
4520
    $v_result = 1;
4521
 
4522
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDirCheck", "entry='$p_dir', is_dir='".($p_is_dir?"true":"false")."'");
4523
 
4524
    // ----- Remove the final '/'
4525
    if (($p_is_dir) && (substr($p_dir, -1)=='/'))
4526
    {
4527
      $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
4528
    }
4529
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Looking for entry '$p_dir'");
4530
 
4531
    // ----- Check the directory availability
4532
    if ((is_dir($p_dir)) || ($p_dir == ""))
4533
    {
4534
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
4535
      return 1;
4536
    }
4537
 
4538
    // ----- Extract parent directory
4539
    $p_parent_dir = dirname($p_dir);
4540
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
4541
 
4542
    // ----- Just a check
4543
    if ($p_parent_dir != $p_dir)
4544
    {
4545
      // ----- Look for parent directory
4546
      if ($p_parent_dir != "")
4547
      {
4548
        if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
4549
        {
4550
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4551
          return $v_result;
4552
        }
4553
      }
4554
    }
4555
 
4556
    // ----- Create the directory
4557
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
4558
    if (!@mkdir($p_dir, 0777))
4559
    {
4560
      // ----- Error log
4561
      PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
4562
 
4563
      // ----- Return
4564
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4565
      return PclZip::errorCode();
4566
    }
4567
 
4568
    // ----- Return
4569
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
4570
    return $v_result;
4571
  }
4572
  // --------------------------------------------------------------------------------
4573
 
4574
  // --------------------------------------------------------------------------------
4575
  // Function : privMerge()
4576
  // Description :
4577
  //   If $p_archive_to_add does not exist, the function exit with a success result.
4578
  // Parameters :
4579
  // Return Values :
4580
  // --------------------------------------------------------------------------------
4581
  function privMerge(&$p_archive_to_add)
4582
  {
4583
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privMerge", "archive='".$p_archive_to_add->zipname."'");
4584
    $v_result=1;
4585
 
4586
    // ----- Look if the archive_to_add exists
4587
    if (!is_file($p_archive_to_add->zipname))
4588
    {
4589
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to add does not exist. End of merge.");
4590
 
4591
      // ----- Nothing to merge, so merge is a success
4592
      $v_result = 1;
4593
 
4594
      // ----- Return
4595
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4596
      return $v_result;
4597
    }
4598
 
4599
    // ----- Look if the archive exists
4600
    if (!is_file($this->zipname))
4601
    {
4602
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, duplicate the archive_to_add.");
4603
 
4604
      // ----- Do a duplicate
4605
      $v_result = $this->privDuplicate($p_archive_to_add->zipname);
4606
 
4607
      // ----- Return
4608
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4609
      return $v_result;
4610
    }
4611
 
4612
    // ----- Open the zip file
4613
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
4614
    if (($v_result=$this->privOpenFd('rb')) != 1)
4615
    {
4616
      // ----- Return
4617
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4618
      return $v_result;
4619
    }
4620
 
4621
    // ----- Read the central directory informations
4622
    $v_central_dir = array();
4623
    if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
4624
    {
4625
      $this->privCloseFd();
4626
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4627
      return $v_result;
4628
    }
4629
 
4630
    // ----- Go to beginning of File
4631
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
4632
    @rewind($this->zip_fd);
4633
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
4634
 
4635
    // ----- Open the archive_to_add file
4636
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open archive_to_add in binary read mode");
4637
    if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
4638
    {
4639
      $this->privCloseFd();
4640
 
4641
      // ----- Return
4642
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4643
      return $v_result;
4644
    }
4645
 
4646
    // ----- Read the central directory informations
4647
    $v_central_dir_to_add = array();
4648
    if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
4649
    {
4650
      $this->privCloseFd();
4651
      $p_archive_to_add->privCloseFd();
4652
 
4653
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4654
      return $v_result;
4655
    }
4656
 
4657
    // ----- Go to beginning of File
4658
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
4659
    @rewind($p_archive_to_add->zip_fd);
4660
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
4661
 
4662
    // ----- Creates a temporay file
4663
    $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
4664
 
4665
    // ----- Open the temporary file in write mode
4666
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
4667
    if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
4668
    {
4669
      $this->privCloseFd();
4670
      $p_archive_to_add->privCloseFd();
4671
 
4672
      PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
4673
 
4674
      // ----- Return
4675
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4676
      return PclZip::errorCode();
4677
    }
4678
 
4679
    // ----- Copy the files from the archive to the temporary file
4680
    // TBC : Here I should better append the file and go back to erase the central dir
4681
    $v_size = $v_central_dir['offset'];
4682
    while ($v_size != 0)
4683
    {
4684
      $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4685
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
4686
      $v_buffer = fread($this->zip_fd, $v_read_size);
4687
      @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
4688
      $v_size -= $v_read_size;
4689
    }
4690
 
4691
    // ----- Copy the files from the archive_to_add into the temporary file
4692
    $v_size = $v_central_dir_to_add['offset'];
4693
    while ($v_size != 0)
4694
    {
4695
      $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4696
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
4697
      $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
4698
      @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
4699
      $v_size -= $v_read_size;
4700
    }
4701
 
4702
    // ----- Store the offset of the central dir
4703
    $v_offset = @ftell($v_zip_temp_fd);
4704
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
4705
 
4706
    // ----- Copy the block of file headers from the old archive
4707
    $v_size = $v_central_dir['size'];
4708
    while ($v_size != 0)
4709
    {
4710
      $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4711
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
4712
      $v_buffer = @fread($this->zip_fd, $v_read_size);
4713
      @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
4714
      $v_size -= $v_read_size;
4715
    }
4716
 
4717
    // ----- Copy the block of file headers from the archive_to_add
4718
    $v_size = $v_central_dir_to_add['size'];
4719
    while ($v_size != 0)
4720
    {
4721
      $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4722
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
4723
      $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
4724
      @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
4725
      $v_size -= $v_read_size;
4726
    }
4727
 
4728
    // ----- Merge the file comments
4729
    $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
4730
 
4731
    // ----- Calculate the size of the (new) central header
4732
    $v_size = @ftell($v_zip_temp_fd)-$v_offset;
4733
 
4734
    // ----- Swap the file descriptor
4735
    // Here is a trick : I swap the temporary fd with the zip fd, in order to use
4736
    // the following methods on the temporary fil and not the real archive fd
4737
    $v_swap = $this->zip_fd;
4738
    $this->zip_fd = $v_zip_temp_fd;
4739
    $v_zip_temp_fd = $v_swap;
4740
 
4741
    // ----- Create the central dir footer
4742
    if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
4743
    {
4744
      $this->privCloseFd();
4745
      $p_archive_to_add->privCloseFd();
4746
      @fclose($v_zip_temp_fd);
4747
      $this->zip_fd = null;
4748
 
4749
      // ----- Reset the file list
4750
      unset($v_header_list);
4751
 
4752
      // ----- Return
4753
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4754
      return $v_result;
4755
    }
4756
 
4757
    // ----- Swap back the file descriptor
4758
    $v_swap = $this->zip_fd;
4759
    $this->zip_fd = $v_zip_temp_fd;
4760
    $v_zip_temp_fd = $v_swap;
4761
 
4762
    // ----- Close
4763
    $this->privCloseFd();
4764
    $p_archive_to_add->privCloseFd();
4765
 
4766
    // ----- Close the temporary file
4767
    @fclose($v_zip_temp_fd);
4768
 
4769
    // ----- Delete the zip file
4770
    // TBC : I should test the result ...
4771
    @unlink($this->zipname);
4772
 
4773
    // ----- Rename the temporary file
4774
    // TBC : I should test the result ...
4775
    //@rename($v_zip_temp_name, $this->zipname);
4776
    PclZipUtilRename($v_zip_temp_name, $this->zipname);
4777
 
4778
    // ----- Return
4779
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4780
    return $v_result;
4781
  }
4782
  // --------------------------------------------------------------------------------
4783
 
4784
  // --------------------------------------------------------------------------------
4785
  // Function : privDuplicate()
4786
  // Description :
4787
  // Parameters :
4788
  // Return Values :
4789
  // --------------------------------------------------------------------------------
4790
  function privDuplicate($p_archive_filename)
4791
  {
4792
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDuplicate", "archive_filename='$p_archive_filename'");
4793
    $v_result=1;
4794
 
4795
    // ----- Look if the $p_archive_filename exists
4796
    if (!is_file($p_archive_filename))
4797
    {
4798
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to duplicate does not exist. End of duplicate.");
4799
 
4800
      // ----- Nothing to duplicate, so duplicate is a success.
4801
      $v_result = 1;
4802
 
4803
      // ----- Return
4804
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4805
      return $v_result;
4806
    }
4807
 
4808
    // ----- Open the zip file
4809
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
4810
    if (($v_result=$this->privOpenFd('wb')) != 1)
4811
    {
4812
      // ----- Return
4813
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4814
      return $v_result;
4815
    }
4816
 
4817
    // ----- Open the temporary file in write mode
4818
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
4819
    if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
4820
    {
4821
      $this->privCloseFd();
4822
 
4823
      PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
4824
 
4825
      // ----- Return
4826
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4827
      return PclZip::errorCode();
4828
    }
4829
 
4830
    // ----- Copy the files from the archive to the temporary file
4831
    // TBC : Here I should better append the file and go back to erase the central dir
4832
    $v_size = filesize($p_archive_filename);
4833
    while ($v_size != 0)
4834
    {
4835
      $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4836
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read $v_read_size bytes");
4837
      $v_buffer = fread($v_zip_temp_fd, $v_read_size);
4838
      @fwrite($this->zip_fd, $v_buffer, $v_read_size);
4839
      $v_size -= $v_read_size;
4840
    }
4841
 
4842
    // ----- Close
4843
    $this->privCloseFd();
4844
 
4845
    // ----- Close the temporary file
4846
    @fclose($v_zip_temp_fd);
4847
 
4848
    // ----- Return
4849
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4850
    return $v_result;
4851
  }
4852
  // --------------------------------------------------------------------------------
4853
 
4854
  // --------------------------------------------------------------------------------
4855
  // Function : privErrorLog()
4856
  // Description :
4857
  // Parameters :
4858
  // --------------------------------------------------------------------------------
4859
  function privErrorLog($p_error_code=0, $p_error_string='')
4860
  {
4861
    if (PCLZIP_ERROR_EXTERNAL == 1) {
4862
      PclError($p_error_code, $p_error_string);
4863
    }
4864
    else {
4865
      $this->error_code = $p_error_code;
4866
      $this->error_string = $p_error_string;
4867
    }
4868
  }
4869
  // --------------------------------------------------------------------------------
4870
 
4871
  // --------------------------------------------------------------------------------
4872
  // Function : privErrorReset()
4873
  // Description :
4874
  // Parameters :
4875
  // --------------------------------------------------------------------------------
4876
  function privErrorReset()
4877
  {
4878
    if (PCLZIP_ERROR_EXTERNAL == 1) {
4879
      PclErrorReset();
4880
    }
4881
    else {
4882
      $this->error_code = 0;
4883
      $this->error_string = '';
4884
    }
4885
  }
4886
  // --------------------------------------------------------------------------------
4887
 
4888
  // --------------------------------------------------------------------------------
4889
  // Function : privDecrypt()
4890
  // Description :
4891
  // Parameters :
4892
  // Return Values :
4893
  // --------------------------------------------------------------------------------
4894
  function privDecrypt($p_encryption_header, &$p_buffer, $p_size, $p_crc)
4895
  {
4896
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privDecrypt', "size=".$p_size."");
4897
    $v_result=1;
4898
 
4899
    // ----- To Be Modified ;-)
4900
    $v_pwd = "test";
4901
 
4902
    $p_buffer = PclZipUtilZipDecrypt($p_buffer, $p_size, $p_encryption_header,
4903
	                                 $p_crc, $v_pwd);
4904
 
4905
    // ----- Return
4906
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4907
    return $v_result;
4908
  }
4909
  // --------------------------------------------------------------------------------
4910
 
4911
  }
4912
  // End of class
4913
  // --------------------------------------------------------------------------------
4914
 
4915
  // --------------------------------------------------------------------------------
4916
  // Function : PclZipUtilPathReduction()
4917
  // Description :
4918
  // Parameters :
4919
  // Return Values :
4920
  // --------------------------------------------------------------------------------
4921
  function PclZipUtilPathReduction($p_dir)
4922
  {
4923
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathReduction", "dir='$p_dir'");
4924
    $v_result = "";
4925
 
4926
    // ----- Look for not empty path
4927
    if ($p_dir != "")
4928
    {
4929
      // ----- Explode path by directory names
4930
      $v_list = explode("/", $p_dir);
4931
 
4932
      // ----- Study directories from last to first
4933
      for ($i=sizeof($v_list)-1; $i>=0; $i--)
4934
      {
4935
        // ----- Look for current path
4936
        if ($v_list[$i] == ".")
4937
        {
4938
          // ----- Ignore this directory
4939
          // Should be the first $i=0, but no check is done
4940
        }
4941
        else if ($v_list[$i] == "..")
4942
        {
4943
          // ----- Ignore it and ignore the $i-1
4944
          $i--;
4945
        }
4946
        else if (($v_list[$i] == "") && ($i!=(sizeof($v_list)-1)) && ($i!=0))
4947
        {
4948
          // ----- Ignore only the double '//' in path,
4949
          // but not the first and last '/'
4950
        }
4951
        else
4952
        {
4953
          $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
4954
        }
4955
      }
4956
    }
4957
 
4958
    // ----- Return
4959
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4960
    return $v_result;
4961
  }
4962
  // --------------------------------------------------------------------------------
4963
 
4964
  // --------------------------------------------------------------------------------
4965
  // Function : PclZipUtilPathInclusion()
4966
  // Description :
4967
  //   This function indicates if the path $p_path is under the $p_dir tree. Or,
4968
  //   said in an other way, if the file or sub-dir $p_path is inside the dir
4969
  //   $p_dir.
4970
  //   The function indicates also if the path is exactly the same as the dir.
4971
  //   This function supports path with duplicated '/' like '//', but does not
4972
  //   support '.' or '..' statements.
4973
  // Parameters :
4974
  // Return Values :
4975
  //   0 if $p_path is not inside directory $p_dir
4976
  //   1 if $p_path is inside directory $p_dir
4977
  //   2 if $p_path is exactly the same as $p_dir
4978
  // --------------------------------------------------------------------------------
4979
  function PclZipUtilPathInclusion($p_dir, $p_path)
4980
  {
4981
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathInclusion", "dir='$p_dir', path='$p_path'");
4982
    $v_result = 1;
4983
 
4984
    // ----- Explode dir and path by directory separator
4985
    $v_list_dir = explode("/", $p_dir);
4986
    $v_list_dir_size = sizeof($v_list_dir);
4987
    $v_list_path = explode("/", $p_path);
4988
    $v_list_path_size = sizeof($v_list_path);
4989
 
4990
    // ----- Study directories paths
4991
    $i = 0;
4992
    $j = 0;
4993
    while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
4994
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Working on dir($i)='".$v_list_dir[$i]."' and path($j)='".$v_list_path[$j]."'");
4995
 
4996
      // ----- Look for empty dir (path reduction)
4997
      if ($v_list_dir[$i] == '') {
4998
        $i++;
4999
        continue;
5000
      }
5001
      if ($v_list_path[$j] == '') {
5002
        $j++;
5003
        continue;
5004
      }
5005
 
5006
      // ----- Compare the items
5007
      if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != ''))  {
5008
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Items ($i,$j) are different");
5009
        $v_result = 0;
5010
      }
5011
 
5012
      // ----- Next items
5013
      $i++;
5014
      $j++;
5015
    }
5016
 
5017
    // ----- Look if everything seems to be the same
5018
    if ($v_result) {
5019
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Look for tie break");
5020
      // ----- Skip all the empty items
5021
      while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
5022
      while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
5023
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Looking on dir($i)='".($i < $v_list_dir_size?$v_list_dir[$i]:'')."' and path($j)='".($j < $v_list_path_size?$v_list_path[$j]:'')."'");
5024
 
5025
      if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
5026
        // ----- There are exactly the same
5027
        $v_result = 2;
5028
      }
5029
      else if ($i < $v_list_dir_size) {
5030
        // ----- The path is shorter than the dir
5031
        $v_result = 0;
5032
      }
5033
    }
5034
 
5035
    // ----- Return
5036
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5037
    return $v_result;
5038
  }
5039
  // --------------------------------------------------------------------------------
5040
 
5041
  // --------------------------------------------------------------------------------
5042
  // Function : PclZipUtilCopyBlock()
5043
  // Description :
5044
  // Parameters :
5045
  //   $p_mode : read/write compression mode
5046
  //             0 : src & dest normal
5047
  //             1 : src gzip, dest normal
5048
  //             2 : src normal, dest gzip
5049
  //             3 : src & dest gzip
5050
  // Return Values :
5051
  // --------------------------------------------------------------------------------
5052
  function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
5053
  {
5054
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilCopyBlock", "size=$p_size, mode=$p_mode");
5055
    $v_result = 1;
5056
 
5057
    if ($p_mode==0)
5058
    {
5059
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset before read :".(@ftell($p_src)));
5060
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset before write :".(@ftell($p_dest)));
5061
      while ($p_size != 0)
5062
      {
5063
        $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5064
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5065
        $v_buffer = @fread($p_src, $v_read_size);
5066
        @fwrite($p_dest, $v_buffer, $v_read_size);
5067
        $p_size -= $v_read_size;
5068
      }
5069
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset after read :".(@ftell($p_src)));
5070
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset after write :".(@ftell($p_dest)));
5071
    }
5072
    else if ($p_mode==1)
5073
    {
5074
      while ($p_size != 0)
5075
      {
5076
        $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5077
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5078
        $v_buffer = @gzread($p_src, $v_read_size);
5079
        @fwrite($p_dest, $v_buffer, $v_read_size);
5080
        $p_size -= $v_read_size;
5081
      }
5082
    }
5083
    else if ($p_mode==2)
5084
    {
5085
      while ($p_size != 0)
5086
      {
5087
        $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5088
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5089
        $v_buffer = @fread($p_src, $v_read_size);
5090
        @gzwrite($p_dest, $v_buffer, $v_read_size);
5091
        $p_size -= $v_read_size;
5092
      }
5093
    }
5094
    else if ($p_mode==3)
5095
    {
5096
      while ($p_size != 0)
5097
      {
5098
        $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5099
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5100
        $v_buffer = @gzread($p_src, $v_read_size);
5101
        @gzwrite($p_dest, $v_buffer, $v_read_size);
5102
        $p_size -= $v_read_size;
5103
      }
5104
    }
5105
 
5106
    // ----- Return
5107
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5108
    return $v_result;
5109
  }
5110
  // --------------------------------------------------------------------------------
5111
 
5112
  // --------------------------------------------------------------------------------
5113
  // Function : PclZipUtilRename()
5114
  // Description :
5115
  //   This function tries to do a simple rename() function. If it fails, it
5116
  //   tries to copy the $p_src file in a new $p_dest file and then unlink the
5117
  //   first one.
5118
  // Parameters :
5119
  //   $p_src : Old filename
5120
  //   $p_dest : New filename
5121
  // Return Values :
5122
  //   1 on success, 0 on failure.
5123
  // --------------------------------------------------------------------------------
5124
  function PclZipUtilRename($p_src, $p_dest)
5125
  {
5126
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilRename", "source=$p_src, destination=$p_dest");
5127
    $v_result = 1;
5128
 
5129
    // ----- Try to rename the files
5130
    if (!@rename($p_src, $p_dest)) {
5131
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to rename file, try copy+unlink");
5132
 
5133
      // ----- Try to copy & unlink the src
5134
      if (!@copy($p_src, $p_dest)) {
5135
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to copy file");
5136
        $v_result = 0;
5137
      }
5138
      else if (!@unlink($p_src)) {
5139
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to unlink old filename");
5140
        $v_result = 0;
5141
      }
5142
    }
5143
 
5144
    // ----- Return
5145
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5146
    return $v_result;
5147
  }
5148
  // --------------------------------------------------------------------------------
5149
 
5150
  // --------------------------------------------------------------------------------
5151
  // Function : PclZipUtilOptionText()
5152
  // Description :
5153
  //   Translate option value in text. Mainly for debug purpose.
5154
  // Parameters :
5155
  //   $p_option : the option value.
5156
  // Return Values :
5157
  //   The option text value.
5158
  // --------------------------------------------------------------------------------
5159
  function PclZipUtilOptionText($p_option)
5160
  {
5161
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilOptionText", "option='".$p_option."'");
5162
 
5163
    $v_list = get_defined_constants();
5164
    for (reset($v_list); $v_key = key($v_list); next($v_list)) {
5165
	  $v_prefix = substr($v_key, 0, 10);
5166
	  if ((($v_prefix == 'PCLZIP_OPT') || ($v_prefix == 'PCLZIP_CB_'))
5167
	      && ($v_list[$v_key] == $p_option)) {
5168
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_key);
5169
          return $v_key;
5170
	    }
5171
    }
5172
 
5173
    $v_result = 'Unknown';
5174
 
5175
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5176
    return $v_result;
5177
  }
5178
  // --------------------------------------------------------------------------------
5179
 
5180
  // --------------------------------------------------------------------------------
5181
  // Function : PclZipUtilTranslateWinPath()
5182
  // Description :
5183
  //   Translate windows path by replacing '\' by '/' and optionally removing
5184
  //   drive letter.
5185
  // Parameters :
5186
  //   $p_path : path to translate.
5187
  //   $p_remove_disk_letter : true | false
5188
  // Return Values :
5189
  //   The path translated.
5190
  // --------------------------------------------------------------------------------
5191
  function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
5192
  {
5193
    if (stristr(php_uname(), 'windows')) {
5194
      // ----- Look for potential disk letter
5195
      if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
5196
          $p_path = substr($p_path, $v_position+1);
5197
      }
5198
      // ----- Change potential windows directory separator
5199
      if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
5200
          $p_path = strtr($p_path, '\\', '/');
5201
      }
5202
    }
5203
    return $p_path;
5204
  }
5205
  // --------------------------------------------------------------------------------
5206
 
5207
 
5208
?>