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