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