Project

General

Profile

« Previous | Next » 

Revision 317

Added by stefan about 18 years ago

Changed all line endings to Unix stlye

View differences:

pclzip.lib.php
1
<?php
2
// --------------------------------------------------------------------------------
3
// PhpConcept Library - Zip Module 2.1
4
// --------------------------------------------------------------------------------
5
// License GNU/LGPL - Vincent Blavet - December 2003
6
// http://www.phpconcept.net
7
// --------------------------------------------------------------------------------
8
//
9
// Presentation :
10
//   PclZip is a PHP library that manage ZIP archives.
11
//   So far tests show that archives generated by PclZip are readable by
12
//   WinZip application and other tools.
13
//
14
// Description :
15
//   See readme.txt and http://www.phpconcept.net
16
//
17
// Warning :
18
//   This library and the associated files are non commercial, non professional
19
//   work.
20
//   It should not have unexpected results. However if any damage is caused by
21
//   this software the author can not be responsible.
22
//   The use of this software is at the risk of the user.
23
//
24
// --------------------------------------------------------------------------------
25
// $Id: pclzip.lib.php,v 1.1.1.1 2005/01/30 10:31:59 rdjurovich Exp $
26
// --------------------------------------------------------------------------------
27

  
28
  // ----- Constants
29
  define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
30
  
31
  // ----- File list separator
32
  // In version 1.x of PclZip, the separator for file list is a space
33
  // (which is not a very smart choice, specifically for windows paths !).
34
  // A better separator should be a comma (,). This constant gives you the
35
  // abilty to change that.
36
  // However notice that changing this value, may have impact on existing
37
  // scripts, using space separated filenames.
38
  // Recommanded values for compatibility with older versions :
39
  //define( 'PCLZIP_SEPARATOR', ' ' );
40
  // Recommanded values for smart separation of filenames.
41
  define( 'PCLZIP_SEPARATOR', ',' );
42

  
43
  // ----- Error configuration
44
  // 0 : PclZip Class integrated error handling
45
  // 1 : PclError external library error handling. By enabling this
46
  //     you must ensure that you have included PclError library.
47
  // [2,...] : reserved for futur use
48
  define( 'PCLZIP_ERROR_EXTERNAL', 0 );
49

  
50
  // ----- Optional static temporary directory
51
  //       By default temporary files are generated in the script current
52
  //       path.
53
  //       If defined :
54
  //       - MUST BE terminated by a '/'.
55
  //       - MUST be a valid, already created directory
56
  //       Samples :
57
  // define( 'PCLZIP_TEMPORARY_DIR', '/temp/' );
58
  // define( 'PCLZIP_TEMPORARY_DIR', 'C:/Temp/' );
59
  define( 'PCLZIP_TEMPORARY_DIR', '' );
60

  
61
// --------------------------------------------------------------------------------
62
// ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
63
// --------------------------------------------------------------------------------
64

  
65
  // ----- Global variables
66
  $g_pclzip_version = "2.1";
67

  
68
  // ----- Error codes
69
  //   -1 : Unable to open file in binary write mode
70
  //   -2 : Unable to open file in binary read mode
71
  //   -3 : Invalid parameters
72
  //   -4 : File does not exist
73
  //   -5 : Filename is too long (max. 255)
74
  //   -6 : Not a valid zip file
75
  //   -7 : Invalid extracted file size
76
  //   -8 : Unable to create directory
77
  //   -9 : Invalid archive extension
78
  //  -10 : Invalid archive format
79
  //  -11 : Unable to delete file (unlink)
80
  //  -12 : Unable to rename file (rename)
81
  //  -13 : Invalid header checksum
82
  //  -14 : Invalid archive size
83
  define( 'PCLZIP_ERR_USER_ABORTED', 2 );
84
  define( 'PCLZIP_ERR_NO_ERROR', 0 );
85
  define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
86
  define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
87
  define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
88
  define( 'PCLZIP_ERR_MISSING_FILE', -4 );
89
  define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
90
  define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
91
  define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
92
  define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
93
  define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
94
  define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
95
  define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
96
  define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
97
  define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
98
  define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
99
  define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
100
  define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
101

  
102
  // ----- Options values
103
  define( 'PCLZIP_OPT_PATH', 77001 );
104
  define( 'PCLZIP_OPT_ADD_PATH', 77002 );
105
  define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
106
  define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
107
  define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
108
  define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
109
  define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
110
  define( 'PCLZIP_OPT_BY_NAME', 77008 );
111
  define( 'PCLZIP_OPT_BY_INDEX', 77009 );
112
  define( 'PCLZIP_OPT_BY_EREG', 77010 );
113
  define( 'PCLZIP_OPT_BY_PREG', 77011 );
114
  define( 'PCLZIP_OPT_COMMENT', 77012 );
115
  define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
116
  define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
117
  define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
118

  
119
  // ----- Call backs values
120
  define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
121
  define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
122
  define( 'PCLZIP_CB_PRE_ADD', 78003 );
123
  define( 'PCLZIP_CB_POST_ADD', 78004 );
124
  /* For futur use
125
  define( 'PCLZIP_CB_PRE_LIST', 78005 );
126
  define( 'PCLZIP_CB_POST_LIST', 78006 );
127
  define( 'PCLZIP_CB_PRE_DELETE', 78007 );
128
  define( 'PCLZIP_CB_POST_DELETE', 78008 );
129
  */
130

  
131
  // --------------------------------------------------------------------------------
132
  // Class : PclZip
133
  // Description :
134
  //   PclZip is the class that represent a Zip archive.
135
  //   The public methods allow the manipulation of the archive.
136
  // Attributes :
137
  //   Attributes must not be accessed directly.
138
  // Methods :
139
  //   PclZip() : Object creator
140
  //   create() : Creates the Zip archive
141
  //   listContent() : List the content of the Zip archive
142
  //   extract() : Extract the content of the archive
143
  //   properties() : List the properties of the archive
144
  // --------------------------------------------------------------------------------
145
  class PclZip
146
  {
147
    // ----- Filename of the zip file
148
    var $zipname = '';
149

  
150
    // ----- File descriptor of the zip file
151
    var $zip_fd = 0;
152

  
153
    // ----- Internal error handling
154
    var $error_code = 1;
155
    var $error_string = '';
156

  
157
  // --------------------------------------------------------------------------------
158
  // Function : PclZip()
159
  // Description :
160
  //   Creates a PclZip object and set the name of the associated Zip archive
161
  //   filename.
162
  //   Note that no real action is taken, if the archive does not exist it is not
163
  //   created. Use create() for that.
164
  // --------------------------------------------------------------------------------
165
  function PclZip($p_zipname)
166
  {
167
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::PclZip', "zipname=$p_zipname");
168

  
169
    // ----- Tests the zlib
170
    if (!function_exists('gzopen'))
171
    {
172
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 1, "zlib extension seems to be missing");
173
      die('Abort '.basename(__FILE__).' : Missing zlib extensions');
174
    }
175

  
176
    // ----- Set the attributes
177
    $this->zipname = $p_zipname;
178
    $this->zip_fd = 0;
179

  
180
    // ----- Return
181
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 1);
182
    return;
183
  }
184
  // --------------------------------------------------------------------------------
185

  
186
  // --------------------------------------------------------------------------------
187
  // Function :
188
  //   create($p_filelist, $p_add_dir="", $p_remove_dir="")
189
  //   create($p_filelist, $p_option, $p_option_value, ...)
190
  // Description :
191
  //   This method supports two different synopsis. The first one is historical.
192
  //   This method creates a Zip Archive. The Zip file is created in the
193
  //   filesystem. The files and directories indicated in $p_filelist
194
  //   are added in the archive. See the parameters description for the
195
  //   supported format of $p_filelist.
196
  //   When a directory is in the list, the directory and its content is added
197
  //   in the archive.
198
  //   In this synopsis, the function takes an optional variable list of
199
  //   options. See bellow the supported options.
200
  // Parameters :
201
  //   $p_filelist : An array containing file or directory names, or
202
  //                 a string containing one filename or one directory name, or
203
  //                 a string containing a list of filenames and/or directory
204
  //                 names separated by spaces.
205
  //   $p_add_dir : A path to add before the real path of the archived file,
206
  //                in order to have it memorized in the archive.
207
  //   $p_remove_dir : A path to remove from the real path of the file to archive,
208
  //                   in order to have a shorter path memorized in the archive.
209
  //                   When $p_add_dir and $p_remove_dir are set, $p_remove_dir
210
  //                   is removed first, before $p_add_dir is added.
211
  // Options :
212
  //   PCLZIP_OPT_ADD_PATH :
213
  //   PCLZIP_OPT_REMOVE_PATH :
214
  //   PCLZIP_OPT_REMOVE_ALL_PATH :
215
  //   PCLZIP_OPT_COMMENT :
216
  //   PCLZIP_CB_PRE_ADD :
217
  //   PCLZIP_CB_POST_ADD :
218
  // Return Values :
219
  //   0 on failure,
220
  //   The list of the added files, with a status of the add action.
221
  //   (see PclZip::listContent() for list entry format)
222
  // --------------------------------------------------------------------------------
223
//  function create($p_filelist, $p_add_dir="", $p_remove_dir="")
224
  function create($p_filelist /*, options */)
225
  {
226
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::create', "filelist='$p_filelist', ...");
227
    $v_result=1;
228

  
229
    // ----- Reset the error handler
230
    $this->privErrorReset();
231

  
232
    // ----- Set default values
233
    $v_options = array();
234
    $v_add_path = "";
235
    $v_remove_path = "";
236
    $v_remove_all_path = false;
237
    $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
238

  
239
    // ----- Look for variable options arguments
240
    $v_size = func_num_args();
241
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
242

  
243
    // ----- Look for arguments
244
    if ($v_size > 1) {
245
      // ----- Get the arguments
246
      $v_arg_list = &func_get_args();
247

  
248
      // ----- Remove form the options list the first argument
249
      array_shift($v_arg_list);
250
      $v_size--;
251

  
252
      // ----- Look for first arg
253
      if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
254
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
255

  
256
        // ----- Parse the options
257
        $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
258
                                            array (PCLZIP_OPT_REMOVE_PATH => 'optional',
259
                                                   PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
260
                                                   PCLZIP_OPT_ADD_PATH => 'optional',
261
                                                   PCLZIP_CB_PRE_ADD => 'optional',
262
                                                   PCLZIP_CB_POST_ADD => 'optional',
263
                                                   PCLZIP_OPT_NO_COMPRESSION => 'optional',
264
                                                   PCLZIP_OPT_COMMENT => 'optional' ));
265
        if ($v_result != 1) {
266
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
267
          return 0;
268
        }
269

  
270
        // ----- Set the arguments
271
        if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
272
          $v_add_path = $v_options[PCLZIP_OPT_ADD_PATH];
273
        }
274
        if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
275
          $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
276
        }
277
        if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
278
          $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
279
        }
280
      }
281

  
282
      // ----- Look for 2 args
283
      // Here we need to support the first historic synopsis of the
284
      // method.
285
      else {
286
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
287

  
288
        // ----- Get the first argument
289
        $v_add_path = $v_arg_list[0];
290

  
291
        // ----- Look for the optional second argument
292
        if ($v_size == 2) {
293
          $v_remove_path = $v_arg_list[1];
294
        }
295
        else if ($v_size > 2) {
296
          // ----- Error log
297
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
298
		                       "Invalid number / type of arguments");
299

  
300
          // ----- Return
301
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
302
          return 0;
303
        }
304
      }
305
    }
306

  
307
    // ----- Trace
308
    //--(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')."'");
309

  
310
    // ----- Look if the $p_filelist is really an array
311
    $p_result_list = array();
312
    if (is_array($p_filelist))
313
    {
314
      // ----- Call the create fct
315
      $v_result = $this->privCreate($p_filelist, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
316
    }
317

  
318
    // ----- Look if the $p_filelist is a string
319
    else if (is_string($p_filelist))
320
    {
321
      // ----- Create a list with the elements from the string
322
      $v_list = explode(PCLZIP_SEPARATOR, $p_filelist);
323

  
324
      // ----- Call the create fct
325
      $v_result = $this->privCreate($v_list, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
326
    }
327

  
328
    // ----- Invalid variable
329
    else
330
    {
331
      // ----- Error log
332
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
333
      $v_result = PCLZIP_ERR_INVALID_PARAMETER;
334
    }
335

  
336
    if ($v_result != 1)
337
    {
338
      // ----- Return
339
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
340
      return 0;
341
    }
342

  
343
    // ----- Return
344
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
345
    return $p_result_list;
346
  }
347
  // --------------------------------------------------------------------------------
348

  
349
  // --------------------------------------------------------------------------------
350
  // Function :
351
  //   add($p_filelist, $p_add_dir="", $p_remove_dir="")
352
  //   add($p_filelist, $p_option, $p_option_value, ...)
353
  // Description :
354
  //   This method supports two synopsis. The first one is historical.
355
  //   This methods add the list of files in an existing archive.
356
  //   If a file with the same name already exists, it is added at the end of the
357
  //   archive, the first one is still present.
358
  //   If the archive does not exist, it is created.
359
  // Parameters :
360
  //   $p_filelist : An array containing file or directory names, or
361
  //                 a string containing one filename or one directory name, or
362
  //                 a string containing a list of filenames and/or directory
363
  //                 names separated by spaces.
364
  //   $p_add_dir : A path to add before the real path of the archived file,
365
  //                in order to have it memorized in the archive.
366
  //   $p_remove_dir : A path to remove from the real path of the file to archive,
367
  //                   in order to have a shorter path memorized in the archive.
368
  //                   When $p_add_dir and $p_remove_dir are set, $p_remove_dir
369
  //                   is removed first, before $p_add_dir is added.
370
  // Options :
371
  //   PCLZIP_OPT_ADD_PATH :
372
  //   PCLZIP_OPT_REMOVE_PATH :
373
  //   PCLZIP_OPT_REMOVE_ALL_PATH :
374
  //   PCLZIP_OPT_COMMENT :
375
  //   PCLZIP_OPT_ADD_COMMENT :
376
  //   PCLZIP_OPT_PREPEND_COMMENT :
377
  //   PCLZIP_CB_PRE_ADD :
378
  //   PCLZIP_CB_POST_ADD :
379
  // Return Values :
380
  //   0 on failure,
381
  //   The list of the added files, with a status of the add action.
382
  //   (see PclZip::listContent() for list entry format)
383
  // --------------------------------------------------------------------------------
384
//  function add($p_filelist, $p_add_dir="", $p_remove_dir="")
385
  function add($p_filelist /* options */)
386
  {
387
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::add', "filelist='$p_filelist', ...");
388
    $v_result=1;
389

  
390
    // ----- Reset the error handler
391
    $this->privErrorReset();
392

  
393
    // ----- Set default values
394
    $v_options = array();
395
    $v_add_path = "";
396
    $v_remove_path = "";
397
    $v_remove_all_path = false;
398
    $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
399

  
400
    // ----- Look for variable options arguments
401
    $v_size = func_num_args();
402
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
403

  
404
    // ----- Look for arguments
405
    if ($v_size > 1) {
406
      // ----- Get the arguments
407
      $v_arg_list = &func_get_args();
408

  
409
      // ----- Remove form the options list the first argument
410
      array_shift($v_arg_list);
411
      $v_size--;
412

  
413
      // ----- Look for first arg
414
      if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
415
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
416

  
417
        // ----- Parse the options
418
        $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
419
                                            array (PCLZIP_OPT_REMOVE_PATH => 'optional',
420
                                                   PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
421
                                                   PCLZIP_OPT_ADD_PATH => 'optional',
422
                                                   PCLZIP_CB_PRE_ADD => 'optional',
423
                                                   PCLZIP_CB_POST_ADD => 'optional',
424
                                                   PCLZIP_OPT_NO_COMPRESSION => 'optional',
425
                                                   PCLZIP_OPT_COMMENT => 'optional',
426
                                                   PCLZIP_OPT_ADD_COMMENT => 'optional',
427
                                                   PCLZIP_OPT_PREPEND_COMMENT => 'optional' ));
428
        if ($v_result != 1) {
429
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
430
          return 0;
431
        }
432

  
433
        // ----- Set the arguments
434
        if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
435
          $v_add_path = $v_options[PCLZIP_OPT_ADD_PATH];
436
        }
437
        if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
438
          $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
439
        }
440
        if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
441
          $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
442
        }
443
      }
444

  
445
      // ----- Look for 2 args
446
      // Here we need to support the first historic synopsis of the
447
      // method.
448
      else {
449
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
450

  
451
        // ----- Get the first argument
452
        $v_add_path = $v_arg_list[0];
453

  
454
        // ----- Look for the optional second argument
455
        if ($v_size == 2) {
456
          $v_remove_path = $v_arg_list[1];
457
        }
458
        else if ($v_size > 2) {
459
          // ----- Error log
460
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
461

  
462
          // ----- Return
463
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
464
          return 0;
465
        }
466
      }
467
    }
468

  
469
    // ----- Trace
470
    //--(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')."'");
471

  
472
    // ----- Look if the $p_filelist is really an array
473
    $p_result_list = array();
474
    if (is_array($p_filelist))
475
    {
476
      // ----- Call the create fct
477
      $v_result = $this->privAdd($p_filelist, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
478
    }
479

  
480
    // ----- Look if the $p_filelist is a string
481
    else if (is_string($p_filelist))
482
    {
483
      // ----- Create a list with the elements from the string
484
      $v_list = explode(PCLZIP_SEPARATOR, $p_filelist);
485

  
486
      // ----- Call the create fct
487
      $v_result = $this->privAdd($v_list, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
488
    }
489

  
490
    // ----- Invalid variable
491
    else
492
    {
493
      // ----- Error log
494
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
495
      $v_result = PCLZIP_ERR_INVALID_PARAMETER;
496
    }
497

  
498
    if ($v_result != 1)
499
    {
500
      // ----- Return
501
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
502
      return 0;
503
    }
504

  
505
    // ----- Return
506
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
507
    return $p_result_list;
508
  }
509
  // --------------------------------------------------------------------------------
510

  
511
  // --------------------------------------------------------------------------------
512
  // Function : listContent()
513
  // Description :
514
  //   This public method, gives the list of the files and directories, with their
515
  //   properties.
516
  //   The properties of each entries in the list are (used also in other functions) :
517
  //     filename : Name of the file. For a create or add action it is the filename
518
  //                given by the user. For an extract function it is the filename
519
  //                of the extracted file.
520
  //     stored_filename : Name of the file / directory stored in the archive.
521
  //     size : Size of the stored file.
522
  //     compressed_size : Size of the file's data compressed in the archive
523
  //                       (without the headers overhead)
524
  //     mtime : Last known modification date of the file (UNIX timestamp)
525
  //     comment : Comment associated with the file
526
  //     folder : true | false
527
  //     index : index of the file in the archive
528
  //     status : status of the action (depending of the action) :
529
  //              Values are :
530
  //                ok : OK !
531
  //                filtered : the file / dir is not extracted (filtered by user)
532
  //                already_a_directory : the file can not be extracted because a
533
  //                                      directory with the same name already exists
534
  //                write_protected : the file can not be extracted because a file
535
  //                                  with the same name already exists and is
536
  //                                  write protected
537
  //                newer_exist : the file was not extracted because a newer file exists
538
  //                path_creation_fail : the file is not extracted because the folder
539
  //                                     does not exists and can not be created
540
  //                write_error : the file was not extracted because there was a
541
  //                              error while writing the file
542
  //                read_error : the file was not extracted because there was a error
543
  //                             while reading the file
544
  //                invalid_header : the file was not extracted because of an archive
545
  //                                 format error (bad file header)
546
  //   Note that each time a method can continue operating when there
547
  //   is an action error on a file, the error is only logged in the file status.
548
  // Return Values :
549
  //   0 on an unrecoverable failure,
550
  //   The list of the files in the archive.
551
  // --------------------------------------------------------------------------------
552
  function listContent()
553
  {
554
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::listContent', "");
555
    $v_result=1;
556

  
557
    // ----- Reset the error handler
558
    $this->privErrorReset();
559

  
560
    // ----- Check archive
561
    if (!$this->privCheckFormat()) {
562
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
563
      return(0);
564
    }
565

  
566
    // ----- Call the extracting fct
567
    $p_list = array();
568
    if (($v_result = $this->privList($p_list)) != 1)
569
    {
570
      unset($p_list);
571
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
572
      return(0);
573
    }
574

  
575
    // ----- Return
576
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
577
    return $p_list;
578
  }
579
  // --------------------------------------------------------------------------------
580

  
581
  // --------------------------------------------------------------------------------
582
  // Function :
583
  //   extract($p_path="./", $p_remove_path="")
584
  //   extract([$p_option, $p_option_value, ...])
585
  // Description :
586
  //   This method supports two synopsis. The first one is historical.
587
  //   This method extract all the files / directories from the archive to the
588
  //   folder indicated in $p_path.
589
  //   If you want to ignore the 'root' part of path of the memorized files
590
  //   you can indicate this in the optional $p_remove_path parameter.
591
  //   By default, if a newer file with the same name already exists, the
592
  //   file is not extracted.
593
  //
594
  //   If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
595
  //   are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
596
  //   at the end of the path value of PCLZIP_OPT_PATH.
597
  // Parameters :
598
  //   $p_path : Path where the files and directories are to be extracted
599
  //   $p_remove_path : First part ('root' part) of the memorized path
600
  //                    (if any similar) to remove while extracting.
601
  // Options :
602
  //   PCLZIP_OPT_PATH :
603
  //   PCLZIP_OPT_ADD_PATH :
604
  //   PCLZIP_OPT_REMOVE_PATH :
605
  //   PCLZIP_OPT_REMOVE_ALL_PATH :
606
  //   PCLZIP_CB_PRE_EXTRACT :
607
  //   PCLZIP_CB_POST_EXTRACT :
608
  // Return Values :
609
  //   0 or a negative value on failure,
610
  //   The list of the extracted files, with a status of the action.
611
  //   (see PclZip::listContent() for list entry format)
612
  // --------------------------------------------------------------------------------
613
  //function extract($p_path="./", $p_remove_path="")
614
  function extract(/* options */)
615
  {
616
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extract", "");
617
    $v_result=1;
618

  
619
    // ----- Reset the error handler
620
    $this->privErrorReset();
621

  
622
    // ----- Check archive
623
    if (!$this->privCheckFormat()) {
624
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
625
      return(0);
626
    }
627

  
628
    // ----- Set default values
629
    $v_options = array();
630
    $v_path = "./";
631
    $v_remove_path = "";
632
    $v_remove_all_path = false;
633

  
634
    // ----- Look for variable options arguments
635
    $v_size = func_num_args();
636
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
637

  
638
    // ----- Default values for option
639
    $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
640

  
641
    // ----- Look for arguments
642
    if ($v_size > 0) {
643
      // ----- Get the arguments
644
      $v_arg_list = &func_get_args();
645

  
646
      // ----- Look for first arg
647
      if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
648
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
649

  
650
        // ----- Parse the options
651
        $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
652
                                            array (PCLZIP_OPT_PATH => 'optional',
653
                                                   PCLZIP_OPT_REMOVE_PATH => 'optional',
654
                                                   PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
655
                                                   PCLZIP_OPT_ADD_PATH => 'optional',
656
                                                   PCLZIP_CB_PRE_EXTRACT => 'optional',
657
                                                   PCLZIP_CB_POST_EXTRACT => 'optional',
658
                                                   PCLZIP_OPT_SET_CHMOD => 'optional',
659
                                                   PCLZIP_OPT_BY_NAME => 'optional',
660
                                                   PCLZIP_OPT_BY_EREG => 'optional',
661
                                                   PCLZIP_OPT_BY_PREG => 'optional',
662
                                                   PCLZIP_OPT_BY_INDEX => 'optional',
663
                                                   PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
664
                                                   PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional' ));
665
        if ($v_result != 1) {
666
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
667
          return 0;
668
        }
669

  
670
        // ----- Set the arguments
671
        if (isset($v_options[PCLZIP_OPT_PATH])) {
672
          $v_path = $v_options[PCLZIP_OPT_PATH];
673
        }
674
        if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
675
          $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
676
        }
677
        if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
678
          $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
679
        }
680
        if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
681
          // ----- Check for '/' in last path char
682
          if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
683
            $v_path .= '/';
684
          }
685
          $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
686
        }
687
      }
688

  
689
      // ----- Look for 2 args
690
      // Here we need to support the first historic synopsis of the
691
      // method.
692
      else {
693
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
694

  
695
        // ----- Get the first argument
696
        $v_path = $v_arg_list[0];
697

  
698
        // ----- Look for the optional second argument
699
        if ($v_size == 2) {
700
          $v_remove_path = $v_arg_list[1];
701
        }
702
        else if ($v_size > 2) {
703
          // ----- Error log
704
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
705

  
706
          // ----- Return
707
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
708
          return 0;
709
        }
710
      }
711
    }
712

  
713
    // ----- Trace
714
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
715

  
716
    // ----- Call the extracting fct
717
    $p_list = array();
718
    $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
719
	                                     $v_remove_all_path, $v_options);
720
    if ($v_result < 1) {
721
      unset($p_list);
722
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
723
      return(0);
724
    }
725

  
726
    // ----- Return
727
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
728
    return $p_list;
729
  }
730
  // --------------------------------------------------------------------------------
731

  
732

  
733
  // --------------------------------------------------------------------------------
734
  // Function :
735
  //   extractByIndex($p_index, $p_path="./", $p_remove_path="")
736
  //   extractByIndex($p_index, [$p_option, $p_option_value, ...])
737
  // Description :
738
  //   This method supports two synopsis. The first one is historical.
739
  //   This method is doing a partial extract of the archive.
740
  //   The extracted files or folders are identified by their index in the
741
  //   archive (from 0 to n).
742
  //   Note that if the index identify a folder, only the folder entry is
743
  //   extracted, not all the files included in the archive.
744
  // Parameters :
745
  //   $p_index : A single index (integer) or a string of indexes of files to
746
  //              extract. The form of the string is "0,4-6,8-12" with only numbers
747
  //              and '-' for range or ',' to separate ranges. No spaces or ';'
748
  //              are allowed.
749
  //   $p_path : Path where the files and directories are to be extracted
750
  //   $p_remove_path : First part ('root' part) of the memorized path
751
  //                    (if any similar) to remove while extracting.
752
  // Options :
753
  //   PCLZIP_OPT_PATH :
754
  //   PCLZIP_OPT_ADD_PATH :
755
  //   PCLZIP_OPT_REMOVE_PATH :
756
  //   PCLZIP_OPT_REMOVE_ALL_PATH :
757
  //   PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
758
  //     not as files.
759
  //     The resulting content is in a new field 'content' in the file
760
  //     structure.
761
  //     This option must be used alone (any other options are ignored).
762
  //   PCLZIP_CB_PRE_EXTRACT :
763
  //   PCLZIP_CB_POST_EXTRACT :
764
  // Return Values :
765
  //   0 on failure,
766
  //   The list of the extracted files, with a status of the action.
767
  //   (see PclZip::listContent() for list entry format)
768
  // --------------------------------------------------------------------------------
769
  function extractByIndex($p_index /* $options */)
770
  {
771
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extractByIndex", "index='$p_index', ...");
772
    $v_result=1;
773

  
774
    // ----- Reset the error handler
775
    $this->privErrorReset();
776

  
777
    // ----- Check archive
778
    if (!$this->privCheckFormat()) {
779
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
780
      return(0);
781
    }
782

  
783
    // ----- Set default values
784
    $v_options = array();
785
    $v_path = "./";
786
    $v_remove_path = "";
787
    $v_remove_all_path = false;
788

  
789
    // ----- Look for variable options arguments
790
    $v_size = func_num_args();
791
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
792

  
793
    // ----- Default values for option
794
    $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
795

  
796
    // ----- Look for arguments
797
    if ($v_size > 1) {
798
      // ----- Get the arguments
799
      $v_arg_list = &func_get_args();
800

  
801
      // ----- Remove form the options list the first argument
802
      array_shift($v_arg_list);
803
      $v_size--;
804

  
805
      // ----- Look for first arg
806
      if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
807
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
808

  
809
        // ----- Parse the options
810
        $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
811
                                            array (PCLZIP_OPT_PATH => 'optional',
812
                                                   PCLZIP_OPT_REMOVE_PATH => 'optional',
813
                                                   PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
814
                                                   PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
815
                                                   PCLZIP_OPT_ADD_PATH => 'optional',
816
                                                   PCLZIP_CB_PRE_EXTRACT => 'optional',
817
                                                   PCLZIP_CB_POST_EXTRACT => 'optional',
818
                                                   PCLZIP_OPT_SET_CHMOD => 'optional' ));
819
        if ($v_result != 1) {
820
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
821
          return 0;
822
        }
823

  
824
        // ----- Set the arguments
825
        if (isset($v_options[PCLZIP_OPT_PATH])) {
826
          $v_path = $v_options[PCLZIP_OPT_PATH];
827
        }
828
        if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
829
          $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
830
        }
831
        if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
832
          $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
833
        }
834
        if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
835
          // ----- Check for '/' in last path char
836
          if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
837
            $v_path .= '/';
838
          }
839
          $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
840
        }
841
        if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
842
          $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
843
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING not set.");
844
        }
845
        else {
846
            //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING set.");
847
        }
848
      }
849

  
850
      // ----- Look for 2 args
851
      // Here we need to support the first historic synopsis of the
852
      // method.
853
      else {
854
        //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
855

  
856
        // ----- Get the first argument
857
        $v_path = $v_arg_list[0];
858

  
859
        // ----- Look for the optional second argument
860
        if ($v_size == 2) {
861
          $v_remove_path = $v_arg_list[1];
862
        }
863
        else if ($v_size > 2) {
864
          // ----- Error log
865
          PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
866

  
867
          // ----- Return
868
          //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
869
          return 0;
870
        }
871
      }
872
    }
873

  
874
    // ----- Trace
875
    //--(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')."'");
876

  
877
    // ----- Trick
878
    // Here I want to reuse extractByRule(), so I need to parse the $p_index
879
    // with privParseOptions()
880
    $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
881
    $v_options_trick = array();
882
    $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
883
                                        array (PCLZIP_OPT_BY_INDEX => 'optional' ));
884
    if ($v_result != 1) {
885
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
886
        return 0;
887
    }
888
    $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
889

  
890
    // ----- Call the extracting fct
891
    if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
892
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
893
        return(0);
894
    }
895

  
896
    // ----- Return
897
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
898
    return $p_list;
899
  }
900
  // --------------------------------------------------------------------------------
901

  
902
  // --------------------------------------------------------------------------------
903
  // Function :
904
  //   delete([$p_option, $p_option_value, ...])
905
  // Description :
906
  // Parameters :
907
  //   None
908
  // Options :
909
  //   PCLZIP_OPT_BY_INDEX :
910
  // Return Values :
911
  //   0 on failure,
912
  //   The list of the files which are still present in the archive.
913
  //   (see PclZip::listContent() for list entry format)
914
  // --------------------------------------------------------------------------------
915
  function delete(/* options */)
916
  {
917
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::delete", "");
918
    $v_result=1;
919

  
920
    // ----- Reset the error handler
921
    $this->privErrorReset();
922

  
923
    // ----- Check archive
924
    if (!$this->privCheckFormat()) {
925
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
926
      return(0);
927
    }
928

  
929
    // ----- Set default values
930
    $v_options = array();
931

  
932
    // ----- Look for variable options arguments
933
    $v_size = func_num_args();
934
    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
935

  
936
    // ----- Look for no arguments
937
    if ($v_size <= 0) {
938
        // ----- Error log
939
        PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing arguments");
940

  
941
        // ----- Return
942
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
943
        return 0;
944
    }
945

  
946
    // ----- Get the arguments
947
    $v_arg_list = &func_get_args();
948

  
949
    // ----- Parse the options
950
    $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
951
                                        array (PCLZIP_OPT_BY_NAME => 'optional',
952
                                               PCLZIP_OPT_BY_EREG => 'optional',
953
                                               PCLZIP_OPT_BY_PREG => 'optional',
954
                                               PCLZIP_OPT_BY_INDEX => 'optional' ));
955
    if ($v_result != 1) {
956
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
957
        return 0;
958
    }
959

  
960
    // ----- Check that at least one rule is set
961
    if (   (!isset($v_options[PCLZIP_OPT_BY_NAME]))
962
        && (!isset($v_options[PCLZIP_OPT_BY_EREG]))
963
        && (!isset($v_options[PCLZIP_OPT_BY_PREG]))
964
        && (!isset($v_options[PCLZIP_OPT_BY_INDEX]))) {
965
        // ----- Error log
966
        PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "At least one filtering rule must be set");
967

  
968
        // ----- Return
969
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
970
        return 0;
971
    }
972

  
973
    // ----- Call the delete fct
974
    $v_list = array();
975
    if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1)
976
    {
977
      unset($v_list);
978
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
979
      return(0);
980
    }
981

  
982
    // ----- Return
983
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_list);
984
    return $v_list;
985
  }
986
  // --------------------------------------------------------------------------------
987

  
988
  // --------------------------------------------------------------------------------
989
  // Function : deleteByIndex()
990
  // Description :
991
  //   ***** Deprecated *****
992
  //   delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
993
  // --------------------------------------------------------------------------------
994
  function deleteByIndex($p_index)
995
  {
996
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::deleteByIndex", "index='$p_index'");
997
    
998
    $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
999

  
1000
    // ----- Return
1001
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
1002
    return $p_list;
1003
  }
1004
  // --------------------------------------------------------------------------------
1005

  
1006
  // --------------------------------------------------------------------------------
1007
  // Function : properties()
1008
  // Description :
1009
  //   This method gives the properties of the archive.
1010
  //   The properties are :
1011
  //     nb : Number of files in the archive
1012
  //     comment : Comment associated with the archive file
1013
  //     status : not_exist, ok
1014
  // Parameters :
1015
  //   None
1016
  // Return Values :
1017
  //   0 on failure,
1018
  //   An array with the archive properties.
1019
  // --------------------------------------------------------------------------------
1020
  function properties()
1021
  {
1022
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::properties", "");
1023

  
1024
    // ----- Reset the error handler
1025
    $this->privErrorReset();
1026

  
1027
    // ----- Check archive
1028
    if (!$this->privCheckFormat()) {
1029
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1030
      return(0);
1031
    }
1032

  
1033
    // ----- Default properties
1034
    $v_prop = array();
1035
    $v_prop['comment'] = '';
1036
    $v_prop['nb'] = 0;
1037
    $v_prop['status'] = 'not_exist';
1038

  
1039
    // ----- Look if file exists
1040
    if (@is_file($this->zipname))
1041
    {
1042
      // ----- Open the zip file
1043
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
1044
      if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
1045
      {
1046
        // ----- Error log
1047
        PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
1048

  
1049
        // ----- Return
1050
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), 0);
1051
        return 0;
1052
      }
1053

  
1054
      // ----- Read the central directory informations
1055
      $v_central_dir = array();
1056
      if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
1057
      {
1058
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1059
        return 0;
1060
      }
1061

  
1062
      // ----- Close the zip file
1063
      $this->privCloseFd();
1064

  
1065
      // ----- Set the user attributes
1066
      $v_prop['comment'] = $v_central_dir['comment'];
1067
      $v_prop['nb'] = $v_central_dir['entries'];
1068
      $v_prop['status'] = 'ok';
1069
    }
1070

  
1071
    // ----- Return
1072
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_prop);
1073
    return $v_prop;
1074
  }
1075
  // --------------------------------------------------------------------------------
1076

  
1077
  // --------------------------------------------------------------------------------
1078
  // Function : duplicate()
1079
  // Description :
1080
  //   This method creates an archive by copying the content of an other one. If
1081
  //   the archive already exist, it is replaced by the new one without any warning.
1082
  // Parameters :
1083
  //   $p_archive : The filename of a valid archive, or
1084
  //                a valid PclZip object.
1085
  // Return Values :
1086
  //   1 on success.
1087
  //   0 or a negative value on error (error code).
1088
  // --------------------------------------------------------------------------------
1089
  function duplicate($p_archive)
1090
  {
1091
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::duplicate", "");
1092
    $v_result = 1;
1093

  
1094
    // ----- Reset the error handler
1095
    $this->privErrorReset();
1096

  
1097
    // ----- Look if the $p_archive is a PclZip object
1098
    if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
1099
    {
1100
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is valid PclZip object '".$p_archive->zipname."'");
1101

  
1102
      // ----- Duplicate the archive
1103
      $v_result = $this->privDuplicate($p_archive->zipname);
1104
    }
1105

  
1106
    // ----- Look if the $p_archive is a string (so a filename)
1107
    else if (is_string($p_archive))
1108
    {
1109
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is a filename '$p_archive'");
1110

  
1111
      // ----- Check that $p_archive is a valid zip file
1112
      // TBC : Should also check the archive format
1113
      if (!is_file($p_archive)) {
1114
        // ----- Error log
1115
        PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
1116
        $v_result = PCLZIP_ERR_MISSING_FILE;
1117
      }
1118
      else {
1119
        // ----- Duplicate the archive
1120
        $v_result = $this->privDuplicate($p_archive);
1121
      }
1122
    }
1123

  
1124
    // ----- Invalid variable
1125
    else
1126
    {
1127
      // ----- Error log
1128
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1129
      $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1130
    }
1131

  
1132
    // ----- Return
1133
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1134
    return $v_result;
1135
  }
1136
  // --------------------------------------------------------------------------------
1137

  
1138
  // --------------------------------------------------------------------------------
1139
  // Function : merge()
1140
  // Description :
1141
  //   This method merge the $p_archive_to_add archive at the end of the current
1142
  //   one ($this).
1143
  //   If the archive ($this) does not exist, the merge becomes a duplicate.
1144
  //   If the $p_archive_to_add archive does not exist, the merge is a success.
1145
  // Parameters :
1146
  //   $p_archive_to_add : It can be directly the filename of a valid zip archive,
1147
  //                       or a PclZip object archive.
1148
  // Return Values :
1149
  //   1 on success,
1150
  //   0 or negative values on error (see below).
1151
  // --------------------------------------------------------------------------------
1152
  function merge($p_archive_to_add)
1153
  {
1154
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::merge", "");
1155
    $v_result = 1;
1156

  
1157
    // ----- Reset the error handler
1158
    $this->privErrorReset();
1159

  
1160
    // ----- Check archive
1161
    if (!$this->privCheckFormat()) {
1162
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1163
      return(0);
1164
    }
1165

  
1166
    // ----- Look if the $p_archive_to_add is a PclZip object
1167
    if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
1168
    {
1169
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is valid PclZip object");
1170

  
1171
      // ----- Merge the archive
1172
      $v_result = $this->privMerge($p_archive_to_add);
1173
    }
1174

  
1175
    // ----- Look if the $p_archive_to_add is a string (so a filename)
1176
    else if (is_string($p_archive_to_add))
1177
    {
1178
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is a filename");
1179

  
1180
      // ----- Create a temporary archive
1181
      $v_object_archive = new PclZip($p_archive_to_add);
1182

  
1183
      // ----- Merge the archive
1184
      $v_result = $this->privMerge($v_object_archive);
1185
    }
1186

  
1187
    // ----- Invalid variable
1188
    else
1189
    {
1190
      // ----- Error log
1191
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1192
      $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1193
    }
1194

  
1195
    // ----- Return
1196
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1197
    return $v_result;
1198
  }
1199
  // --------------------------------------------------------------------------------
1200

  
1201

  
1202

  
1203
  // --------------------------------------------------------------------------------
1204
  // Function : errorCode()
1205
  // Description :
1206
  // Parameters :
1207
  // --------------------------------------------------------------------------------
1208
  function errorCode()
1209
  {
1210
    if (PCLZIP_ERROR_EXTERNAL == 1) {
1211
      return(PclErrorCode());
1212
    }
1213
    else {
1214
      return($this->error_code);
1215
    }
1216
  }
1217
  // --------------------------------------------------------------------------------
1218

  
1219
  // --------------------------------------------------------------------------------
1220
  // Function : errorName()
1221
  // Description :
1222
  // Parameters :
1223
  // --------------------------------------------------------------------------------
1224
  function errorName($p_with_code=false)
1225
  {
1226
    $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
1227
                      PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
1228
                      PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
1229
                      PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
1230
                      PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
1231
                      PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
1232
                      PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
1233
                      PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
1234
                      PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
1235
                      PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
1236
                      PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
1237
                      PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
1238
                      PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
1239
                      PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
1240
                      PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
1241
                      PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
1242
                      PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE' );
1243

  
1244
    if (isset($v_name[$this->error_code])) {
1245
      $v_value = $v_name[$this->error_code];
1246
    }
1247
    else {
1248
      $v_value = 'NoName';
1249
    }
1250

  
1251
    if ($p_with_code) {
1252
      return($v_value.' ('.$this->error_code.')');
1253
    }
1254
    else {
1255
      return($v_value);
1256
    }
1257
  }
1258
  // --------------------------------------------------------------------------------
1259

  
1260
  // --------------------------------------------------------------------------------
1261
  // Function : errorInfo()
1262
  // Description :
1263
  // Parameters :
1264
  // --------------------------------------------------------------------------------
1265
  function errorInfo($p_full=false)
1266
  {
1267
    if (PCLZIP_ERROR_EXTERNAL == 1) {
1268
      return(PclErrorString());
1269
    }
1270
    else {
1271
      if ($p_full) {
1272
        return($this->errorName(true)." : ".$this->error_string);
1273
      }
1274
      else {
1275
        return($this->error_string." [code ".$this->error_code."]");
1276
      }
1277
    }
1278
  }
1279
  // --------------------------------------------------------------------------------
1280

  
1281

  
1282
// --------------------------------------------------------------------------------
1283
// ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1284
// *****                                                        *****
1285
// *****       THESES FUNCTIONS MUST NOT BE USED DIRECTLY       *****
1286
// --------------------------------------------------------------------------------
1287

  
1288

  
1289

  
1290
  // --------------------------------------------------------------------------------
1291
  // Function : privCheckFormat()
1292
  // Description :
1293
  //   This method check that the archive exists and is a valid zip archive.
1294
  //   Several level of check exists. (futur)
1295
  // Parameters :
1296
  //   $p_level : Level of check. Default 0.
1297
  //              0 : Check the first bytes (magic codes) (default value))
1298
  //              1 : 0 + Check the central directory (futur)
1299
  //              2 : 1 + Check each file header (futur)
1300
  // Return Values :
1301
  //   true on success,
1302
  //   false on error, the error code is set.
1303
  // --------------------------------------------------------------------------------
1304
  function privCheckFormat($p_level=0)
1305
  {
1306
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFormat", "");
1307
    $v_result = true;
1308

  
1309
	// ----- Reset the file system cache
1310
    clearstatcache();
1311

  
1312
    // ----- Reset the error handler
1313
    $this->privErrorReset();
1314

  
1315
    // ----- Look if the file exits
1316
    if (!is_file($this->zipname)) {
1317
      // ----- Error log
1318
      PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
1319
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1320
      return(false);
1321
    }
1322

  
1323
    // ----- Check that the file is readeable
1324
    if (!is_readable($this->zipname)) {
1325
      // ----- Error log
1326
      PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
1327
      //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1328
      return(false);
1329
    }
1330

  
1331
    // ----- Check the magic code
1332
    // TBC
1333

  
1334
    // ----- Check the central header
1335
    // TBC
1336

  
1337
    // ----- Check each file header
1338
    // TBC
1339

  
1340
    // ----- Return
1341
    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1342
    return $v_result;
1343
  }
1344
  // --------------------------------------------------------------------------------
1345

  
1346
  // --------------------------------------------------------------------------------
1347
  // Function : privParseOptions()
1348
  // Description :
1349
  //   This internal methods reads the variable list of arguments ($p_options_list,
1350
  //   $p_size) and generate an array with the options and values ($v_result_list).
1351
  //   $v_requested_options contains the options that can be present and those that
1352
  //   must be present.
1353
  //   $v_requested_options is an array, with the option value as key, and 'optional',
1354
  //   or 'mandatory' as value.
1355
  // Parameters :
1356
  //   See above.
1357
  // Return Values :
1358
  //   1 on success.
1359
  //   0 on failure.
1360
  // --------------------------------------------------------------------------------
1361
  function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
1362
  {
1363
    //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privParseOptions", "");
1364
    $v_result=1;
1365

  
1366
    // ----- Read the options
1367
    $i=0;
1368
    while ($i<$p_size) {
1369
      //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Looking for table index $i, option = '".PclZipUtilOptionText($p_options_list[$i])."(".$p_options_list[$i].")'");
1370

  
1371
      // ----- Check if the option is requested
1372
      if (!isset($v_requested_options[$p_options_list[$i]])) {
1373
        // ----- Error log
1374
        PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
1375

  
1376
        // ----- Return
1377
        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1378
        return PclZip::errorCode();
1379
      }
1380

  
1381
      // ----- Look for next option
1382
      switch ($p_options_list[$i]) {
1383
        // ----- Look for options that request a path value
1384
        case PCLZIP_OPT_PATH :
1385
        case PCLZIP_OPT_REMOVE_PATH :
1386
        case PCLZIP_OPT_ADD_PATH :
1387
          // ----- Check the number of parameters
1388
          if (($i+1) >= $p_size) {
1389
            // ----- Error log
1390
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1391

  
1392
            // ----- Return
1393
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1394
            return PclZip::errorCode();
1395
          }
1396

  
1397
          // ----- Get the value
1398
          $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], false);
1399
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1400
          $i++;
1401
        break;
1402

  
1403
        // ----- Look for options that request an array of string for value
1404
        case PCLZIP_OPT_BY_NAME :
1405
          // ----- Check the number of parameters
1406
          if (($i+1) >= $p_size) {
1407
            // ----- Error log
1408
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1409

  
1410
            // ----- Return
1411
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1412
            return PclZip::errorCode();
1413
          }
1414

  
1415
          // ----- Get the value
1416
          if (is_string($p_options_list[$i+1])) {
1417
              $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
1418
          }
1419
          else if (is_array($p_options_list[$i+1])) {
1420
              $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1421
          }
1422
          else {
1423
            // ----- Error log
1424
            PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1425

  
1426
            // ----- Return
1427
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1428
            return PclZip::errorCode();
1429
          }
1430
          ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1431
          $i++;
1432
        break;
1433

  
1434
        // ----- Look for options that request an EREG or PREG expression
1435
        case PCLZIP_OPT_BY_EREG :
1436
        case PCLZIP_OPT_BY_PREG :
1437
          // ----- Check the number of parameters
1438
          if (($i+1) >= $p_size) {
1439
            // ----- Error log
1440
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1441

  
1442
            // ----- Return
1443
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1444
            return PclZip::errorCode();
1445
          }
1446

  
1447
          // ----- Get the value
1448
          if (is_string($p_options_list[$i+1])) {
1449
              $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1450
          }
1451
          else {
1452
            // ----- Error log
1453
            PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1454

  
1455
            // ----- Return
1456
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1457
            return PclZip::errorCode();
1458
          }
1459
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1460
          $i++;
1461
        break;
1462

  
1463
        // ----- Look for options that takes a string
1464
        case PCLZIP_OPT_COMMENT :
1465
        case PCLZIP_OPT_ADD_COMMENT :
1466
        case PCLZIP_OPT_PREPEND_COMMENT :
1467
          // ----- Check the number of parameters
1468
          if (($i+1) >= $p_size) {
1469
            // ----- Error log
1470
            PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
1471
			                     "Missing parameter value for option '"
1472
								 .PclZipUtilOptionText($p_options_list[$i])
1473
								 ."'");
1474

  
1475
            // ----- Return
1476
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1477
            return PclZip::errorCode();
1478
          }
1479

  
1480
          // ----- Get the value
1481
          if (is_string($p_options_list[$i+1])) {
1482
              $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1483
          }
1484
          else {
1485
            // ----- Error log
1486
            PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
1487
			                     "Wrong parameter value for option '"
1488
								 .PclZipUtilOptionText($p_options_list[$i])
1489
								 ."'");
1490

  
1491
            // ----- Return
1492
            //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1493
            return PclZip::errorCode();
1494
          }
1495
          //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1496
          $i++;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff