Added ability to specify destination category within GIFT file.
[moodle-linuxchix.git] / lib / pclzip / pclzip.lib.php
blob5b38e69dcdfdda65abb19813e9abfe24a177a626
1 <?php
2 // --------------------------------------------------------------------------------
3 // PhpConcept Library - Zip Module 2.5
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.
14 // Description :
15 // See readme.txt and http://www.phpconcept.net
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.
24 // --------------------------------------------------------------------------------
25 // $Id$
26 // --------------------------------------------------------------------------------
28 // ----- Constants
29 define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
31 // ----- File list separator
32 // In version 1.x of PclZip, the separator for file list is a space
33 // (which is not a very smart choice, specifically for windows paths !).
34 // A better separator should be a comma (,). This constant gives you the
35 // abilty to change that.
36 // However notice that changing this value, may have impact on existing
37 // scripts, using space separated filenames.
38 // Recommanded values for compatibility with older versions :
39 //define( 'PCLZIP_SEPARATOR', ' ' );
40 // Recommanded values for smart separation of filenames.
41 define( 'PCLZIP_SEPARATOR', ',' );
43 // ----- Error configuration
44 // 0 : PclZip Class integrated error handling
45 // 1 : PclError external library error handling. By enabling this
46 // you must ensure that you have included PclError library.
47 // [2,...] : reserved for futur use
48 define( 'PCLZIP_ERROR_EXTERNAL', 0 );
50 // ----- Optional static temporary directory
51 // By default temporary files are generated in the script current
52 // path.
53 // If defined :
54 // - MUST BE terminated by a '/'.
55 // - MUST be a valid, already created directory
56 // Samples :
57 // define( 'PCLZIP_TEMPORARY_DIR', '/temp/' );
58 // define( 'PCLZIP_TEMPORARY_DIR', 'C:/Temp/' );
59 define( 'PCLZIP_TEMPORARY_DIR', '' );
61 // --------------------------------------------------------------------------------
62 // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
63 // --------------------------------------------------------------------------------
65 // ----- Global variables
66 $g_pclzip_version = "2.5";
68 // ----- Error codes
69 // -1 : Unable to open file in binary write mode
70 // -2 : Unable to open file in binary read mode
71 // -3 : Invalid parameters
72 // -4 : File does not exist
73 // -5 : Filename is too long (max. 255)
74 // -6 : Not a valid zip file
75 // -7 : Invalid extracted file size
76 // -8 : Unable to create directory
77 // -9 : Invalid archive extension
78 // -10 : Invalid archive format
79 // -11 : Unable to delete file (unlink)
80 // -12 : Unable to rename file (rename)
81 // -13 : Invalid header checksum
82 // -14 : Invalid archive size
83 define( 'PCLZIP_ERR_USER_ABORTED', 2 );
84 define( 'PCLZIP_ERR_NO_ERROR', 0 );
85 define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
86 define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
87 define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
88 define( 'PCLZIP_ERR_MISSING_FILE', -4 );
89 define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
90 define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
91 define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
92 define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
93 define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
94 define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
95 define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
96 define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
97 define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
98 define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
99 define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
100 define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
101 define( 'PCLZIP_ERR_ALREADY_A_DIRECTORY', -17 );
102 define( 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18 );
103 define( 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19 );
104 define( 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20 );
105 define( 'PCLZIP_ERR_DIRECTORY_RESTRICTION', -21 );
107 // ----- Options values
108 define( 'PCLZIP_OPT_PATH', 77001 );
109 define( 'PCLZIP_OPT_ADD_PATH', 77002 );
110 define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
111 define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
112 define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
113 define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
114 define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
115 define( 'PCLZIP_OPT_BY_NAME', 77008 );
116 define( 'PCLZIP_OPT_BY_INDEX', 77009 );
117 define( 'PCLZIP_OPT_BY_EREG', 77010 );
118 define( 'PCLZIP_OPT_BY_PREG', 77011 );
119 define( 'PCLZIP_OPT_COMMENT', 77012 );
120 define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
121 define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
122 define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
123 define( 'PCLZIP_OPT_REPLACE_NEWER', 77016 );
124 define( 'PCLZIP_OPT_STOP_ON_ERROR', 77017 );
125 // Having big trouble with crypt. Need to multiply 2 long int
126 // which is not correctly supported by PHP ...
127 //define( 'PCLZIP_OPT_CRYPT', 77018 );
128 define( 'PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019 );
130 // ----- File description attributes
131 define( 'PCLZIP_ATT_FILE_NAME', 79001 );
132 define( 'PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002 );
133 define( 'PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003 );
135 // ----- Call backs values
136 define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
137 define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
138 define( 'PCLZIP_CB_PRE_ADD', 78003 );
139 define( 'PCLZIP_CB_POST_ADD', 78004 );
140 /* For futur use
141 define( 'PCLZIP_CB_PRE_LIST', 78005 );
142 define( 'PCLZIP_CB_POST_LIST', 78006 );
143 define( 'PCLZIP_CB_PRE_DELETE', 78007 );
144 define( 'PCLZIP_CB_POST_DELETE', 78008 );
147 // --------------------------------------------------------------------------------
148 // Class : PclZip
149 // Description :
150 // PclZip is the class that represent a Zip archive.
151 // The public methods allow the manipulation of the archive.
152 // Attributes :
153 // Attributes must not be accessed directly.
154 // Methods :
155 // PclZip() : Object creator
156 // create() : Creates the Zip archive
157 // listContent() : List the content of the Zip archive
158 // extract() : Extract the content of the archive
159 // properties() : List the properties of the archive
160 // --------------------------------------------------------------------------------
161 class PclZip
163 // ----- Filename of the zip file
164 var $zipname = '';
166 // ----- File descriptor of the zip file
167 var $zip_fd = 0;
169 // ----- Internal error handling
170 var $error_code = 1;
171 var $error_string = '';
173 // ----- Current status of the magic_quotes_runtime
174 // This value store the php configuration for magic_quotes
175 // The class can then disable the magic_quotes and reset it after
176 var $magic_quotes_status;
178 // --------------------------------------------------------------------------------
179 // Function : PclZip()
180 // Description :
181 // Creates a PclZip object and set the name of the associated Zip archive
182 // filename.
183 // Note that no real action is taken, if the archive does not exist it is not
184 // created. Use create() for that.
185 // --------------------------------------------------------------------------------
186 function PclZip($p_zipname)
188 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::PclZip', "zipname=$p_zipname");
190 // ----- Tests the zlib
191 if (!function_exists('gzopen'))
193 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 1, "zlib extension seems to be missing");
194 die('Abort '.basename(__FILE__).' : Missing zlib extensions');
197 // ----- Set the attributes
198 $this->zipname = $p_zipname;
199 $this->zip_fd = 0;
200 $this->magic_quotes_status = -1;
202 // ----- Return
203 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 1);
204 return;
206 // --------------------------------------------------------------------------------
208 // --------------------------------------------------------------------------------
209 // Function :
210 // create($p_filelist, $p_add_dir="", $p_remove_dir="")
211 // create($p_filelist, $p_option, $p_option_value, ...)
212 // Description :
213 // This method supports two different synopsis. The first one is historical.
214 // This method creates a Zip Archive. The Zip file is created in the
215 // filesystem. The files and directories indicated in $p_filelist
216 // are added in the archive. See the parameters description for the
217 // supported format of $p_filelist.
218 // When a directory is in the list, the directory and its content is added
219 // in the archive.
220 // In this synopsis, the function takes an optional variable list of
221 // options. See bellow the supported options.
222 // Parameters :
223 // $p_filelist : An array containing file or directory names, or
224 // a string containing one filename or one directory name, or
225 // a string containing a list of filenames and/or directory
226 // names separated by spaces.
227 // $p_add_dir : A path to add before the real path of the archived file,
228 // in order to have it memorized in the archive.
229 // $p_remove_dir : A path to remove from the real path of the file to archive,
230 // in order to have a shorter path memorized in the archive.
231 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
232 // is removed first, before $p_add_dir is added.
233 // Options :
234 // PCLZIP_OPT_ADD_PATH :
235 // PCLZIP_OPT_REMOVE_PATH :
236 // PCLZIP_OPT_REMOVE_ALL_PATH :
237 // PCLZIP_OPT_COMMENT :
238 // PCLZIP_CB_PRE_ADD :
239 // PCLZIP_CB_POST_ADD :
240 // Return Values :
241 // 0 on failure,
242 // The list of the added files, with a status of the add action.
243 // (see PclZip::listContent() for list entry format)
244 // --------------------------------------------------------------------------------
245 function create($p_filelist)
247 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::create', "filelist='$p_filelist', ...");
248 $v_result=1;
250 // ----- Reset the error handler
251 $this->privErrorReset();
253 // ----- Set default values
254 $v_options = array();
255 $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
257 // ----- Look for variable options arguments
258 $v_size = func_num_args();
259 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
261 // ----- Look for arguments
262 if ($v_size > 1) {
263 // ----- Get the arguments
264 $v_arg_list = func_get_args();
266 // ----- Remove from the options list the first argument
267 array_shift($v_arg_list);
268 $v_size--;
270 // ----- Look for first arg
271 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
272 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
274 // ----- Parse the options
275 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
276 array (PCLZIP_OPT_REMOVE_PATH => 'optional',
277 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
278 PCLZIP_OPT_ADD_PATH => 'optional',
279 PCLZIP_CB_PRE_ADD => 'optional',
280 PCLZIP_CB_POST_ADD => 'optional',
281 PCLZIP_OPT_NO_COMPRESSION => 'optional',
282 PCLZIP_OPT_COMMENT => 'optional'
283 //, PCLZIP_OPT_CRYPT => 'optional'
285 if ($v_result != 1) {
286 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
287 return 0;
291 // ----- Look for 2 args
292 // Here we need to support the first historic synopsis of the
293 // method.
294 else {
295 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
297 // ----- Get the first argument
298 $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
300 // ----- Look for the optional second argument
301 if ($v_size == 2) {
302 $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
304 else if ($v_size > 2) {
305 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
306 "Invalid number / type of arguments");
307 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
308 return 0;
313 // ----- Init
314 $v_string_list = array();
315 $v_att_list = array();
316 $v_filedescr_list = array();
317 $p_result_list = array();
319 // ----- Look if the $p_filelist is really an array
320 if (is_array($p_filelist)) {
322 // ----- Look if the first element is also an array
323 // This will mean that this is a file description entry
324 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
325 $v_att_list = $p_filelist;
328 // ----- The list is a list of string names
329 else {
330 $v_string_list = $p_filelist;
334 // ----- Look if the $p_filelist is a string
335 else if (is_string($p_filelist)) {
336 // ----- Create a list from the string
337 $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
340 // ----- Invalid variable type for $p_filelist
341 else {
342 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
343 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
344 return 0;
347 // ----- Reformat the string list
348 if (sizeof($v_string_list) != 0) {
349 foreach ($v_string_list as $v_string) {
350 if ($v_string != '') {
351 $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
353 else {
354 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Ignore an empty filename");
359 // ----- For each file in the list check the attributes
360 $v_supported_attributes
361 = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
362 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
363 ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
365 foreach ($v_att_list as $v_entry) {
366 $v_result = $this->privFileDescrParseAtt($v_entry,
367 $v_filedescr_list[],
368 $v_options,
369 $v_supported_attributes);
370 if ($v_result != 1) {
371 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
372 return 0;
376 // ----- Expand the filelist (expand directories)
377 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
378 if ($v_result != 1) {
379 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
380 return 0;
383 // ----- Call the create fct
384 $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
385 if ($v_result != 1) {
386 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
387 return 0;
390 // ----- Return
391 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
392 return $p_result_list;
394 // --------------------------------------------------------------------------------
396 // --------------------------------------------------------------------------------
397 // Function :
398 // add($p_filelist, $p_add_dir="", $p_remove_dir="")
399 // add($p_filelist, $p_option, $p_option_value, ...)
400 // Description :
401 // This method supports two synopsis. The first one is historical.
402 // This methods add the list of files in an existing archive.
403 // If a file with the same name already exists, it is added at the end of the
404 // archive, the first one is still present.
405 // If the archive does not exist, it is created.
406 // Parameters :
407 // $p_filelist : An array containing file or directory names, or
408 // a string containing one filename or one directory name, or
409 // a string containing a list of filenames and/or directory
410 // names separated by spaces.
411 // $p_add_dir : A path to add before the real path of the archived file,
412 // in order to have it memorized in the archive.
413 // $p_remove_dir : A path to remove from the real path of the file to archive,
414 // in order to have a shorter path memorized in the archive.
415 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
416 // is removed first, before $p_add_dir is added.
417 // Options :
418 // PCLZIP_OPT_ADD_PATH :
419 // PCLZIP_OPT_REMOVE_PATH :
420 // PCLZIP_OPT_REMOVE_ALL_PATH :
421 // PCLZIP_OPT_COMMENT :
422 // PCLZIP_OPT_ADD_COMMENT :
423 // PCLZIP_OPT_PREPEND_COMMENT :
424 // PCLZIP_CB_PRE_ADD :
425 // PCLZIP_CB_POST_ADD :
426 // Return Values :
427 // 0 on failure,
428 // The list of the added files, with a status of the add action.
429 // (see PclZip::listContent() for list entry format)
430 // --------------------------------------------------------------------------------
431 function add($p_filelist)
433 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::add', "filelist='$p_filelist', ...");
434 $v_result=1;
436 // ----- Reset the error handler
437 $this->privErrorReset();
439 // ----- Set default values
440 $v_options = array();
441 $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
443 // ----- Look for variable options arguments
444 $v_size = func_num_args();
445 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
447 // ----- Look for arguments
448 if ($v_size > 1) {
449 // ----- Get the arguments
450 $v_arg_list = func_get_args();
452 // ----- Remove form the options list the first argument
453 array_shift($v_arg_list);
454 $v_size--;
456 // ----- Look for first arg
457 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
458 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
460 // ----- Parse the options
461 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
462 array (PCLZIP_OPT_REMOVE_PATH => 'optional',
463 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
464 PCLZIP_OPT_ADD_PATH => 'optional',
465 PCLZIP_CB_PRE_ADD => 'optional',
466 PCLZIP_CB_POST_ADD => 'optional',
467 PCLZIP_OPT_NO_COMPRESSION => 'optional',
468 PCLZIP_OPT_COMMENT => 'optional',
469 PCLZIP_OPT_ADD_COMMENT => 'optional',
470 PCLZIP_OPT_PREPEND_COMMENT => 'optional'
471 //, PCLZIP_OPT_CRYPT => 'optional'
473 if ($v_result != 1) {
474 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
475 return 0;
479 // ----- Look for 2 args
480 // Here we need to support the first historic synopsis of the
481 // method.
482 else {
483 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
485 // ----- Get the first argument
486 $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
488 // ----- Look for the optional second argument
489 if ($v_size == 2) {
490 $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
492 else if ($v_size > 2) {
493 // ----- Error log
494 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
496 // ----- Return
497 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
498 return 0;
503 // ----- Init
504 $v_string_list = array();
505 $v_att_list = array();
506 $v_filedescr_list = array();
507 $p_result_list = array();
509 // ----- Look if the $p_filelist is really an array
510 if (is_array($p_filelist)) {
512 // ----- Look if the first element is also an array
513 // This will mean that this is a file description entry
514 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
515 $v_att_list = $p_filelist;
518 // ----- The list is a list of string names
519 else {
520 $v_string_list = $p_filelist;
524 // ----- Look if the $p_filelist is a string
525 else if (is_string($p_filelist)) {
526 // ----- Create a list from the string
527 $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
530 // ----- Invalid variable type for $p_filelist
531 else {
532 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
533 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
534 return 0;
537 // ----- Reformat the string list
538 if (sizeof($v_string_list) != 0) {
539 foreach ($v_string_list as $v_string) {
540 $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
544 // ----- For each file in the list check the attributes
545 $v_supported_attributes
546 = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
547 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
548 ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
550 foreach ($v_att_list as $v_entry) {
551 $v_result = $this->privFileDescrParseAtt($v_entry,
552 $v_filedescr_list[],
553 $v_options,
554 $v_supported_attributes);
555 if ($v_result != 1) {
556 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
557 return 0;
561 // ----- Expand the filelist (expand directories)
562 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
563 if ($v_result != 1) {
564 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
565 return 0;
568 // ----- Call the create fct
569 $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
570 if ($v_result != 1) {
571 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
572 return 0;
575 // ----- Return
576 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
577 return $p_result_list;
579 // --------------------------------------------------------------------------------
581 // --------------------------------------------------------------------------------
582 // Function : listContent()
583 // Description :
584 // This public method, gives the list of the files and directories, with their
585 // properties.
586 // The properties of each entries in the list are (used also in other functions) :
587 // filename : Name of the file. For a create or add action it is the filename
588 // given by the user. For an extract function it is the filename
589 // of the extracted file.
590 // stored_filename : Name of the file / directory stored in the archive.
591 // size : Size of the stored file.
592 // compressed_size : Size of the file's data compressed in the archive
593 // (without the headers overhead)
594 // mtime : Last known modification date of the file (UNIX timestamp)
595 // comment : Comment associated with the file
596 // folder : true | false
597 // index : index of the file in the archive
598 // status : status of the action (depending of the action) :
599 // Values are :
600 // ok : OK !
601 // filtered : the file / dir is not extracted (filtered by user)
602 // already_a_directory : the file can not be extracted because a
603 // directory with the same name already exists
604 // write_protected : the file can not be extracted because a file
605 // with the same name already exists and is
606 // write protected
607 // newer_exist : the file was not extracted because a newer file exists
608 // path_creation_fail : the file is not extracted because the folder
609 // does not exists and can not be created
610 // write_error : the file was not extracted because there was a
611 // error while writing the file
612 // read_error : the file was not extracted because there was a error
613 // while reading the file
614 // invalid_header : the file was not extracted because of an archive
615 // format error (bad file header)
616 // Note that each time a method can continue operating when there
617 // is an action error on a file, the error is only logged in the file status.
618 // Return Values :
619 // 0 on an unrecoverable failure,
620 // The list of the files in the archive.
621 // --------------------------------------------------------------------------------
622 function listContent()
624 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::listContent', "");
625 $v_result=1;
627 // ----- Reset the error handler
628 $this->privErrorReset();
630 // ----- Check archive
631 if (!$this->privCheckFormat()) {
632 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
633 return(0);
636 // ----- Call the extracting fct
637 $p_list = array();
638 if (($v_result = $this->privList($p_list)) != 1)
640 unset($p_list);
641 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
642 return(0);
645 // ----- Return
646 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
647 return $p_list;
649 // --------------------------------------------------------------------------------
651 // --------------------------------------------------------------------------------
652 // Function :
653 // extract($p_path="./", $p_remove_path="")
654 // extract([$p_option, $p_option_value, ...])
655 // Description :
656 // This method supports two synopsis. The first one is historical.
657 // This method extract all the files / directories from the archive to the
658 // folder indicated in $p_path.
659 // If you want to ignore the 'root' part of path of the memorized files
660 // you can indicate this in the optional $p_remove_path parameter.
661 // By default, if a newer file with the same name already exists, the
662 // file is not extracted.
664 // If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
665 // are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
666 // at the end of the path value of PCLZIP_OPT_PATH.
667 // Parameters :
668 // $p_path : Path where the files and directories are to be extracted
669 // $p_remove_path : First part ('root' part) of the memorized path
670 // (if any similar) to remove while extracting.
671 // Options :
672 // PCLZIP_OPT_PATH :
673 // PCLZIP_OPT_ADD_PATH :
674 // PCLZIP_OPT_REMOVE_PATH :
675 // PCLZIP_OPT_REMOVE_ALL_PATH :
676 // PCLZIP_CB_PRE_EXTRACT :
677 // PCLZIP_CB_POST_EXTRACT :
678 // Return Values :
679 // 0 or a negative value on failure,
680 // The list of the extracted files, with a status of the action.
681 // (see PclZip::listContent() for list entry format)
682 // --------------------------------------------------------------------------------
683 function extract()
685 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extract", "");
686 $v_result=1;
688 // ----- Reset the error handler
689 $this->privErrorReset();
691 // ----- Check archive
692 if (!$this->privCheckFormat()) {
693 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
694 return(0);
697 // ----- Set default values
698 $v_options = array();
699 // $v_path = "./";
700 $v_path = '';
701 $v_remove_path = "";
702 $v_remove_all_path = false;
704 // ----- Look for variable options arguments
705 $v_size = func_num_args();
706 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
708 // ----- Default values for option
709 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
711 // ----- Look for arguments
712 if ($v_size > 0) {
713 // ----- Get the arguments
714 $v_arg_list = func_get_args();
716 // ----- Look for first arg
717 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
718 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
720 // ----- Parse the options
721 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
722 array (PCLZIP_OPT_PATH => 'optional',
723 PCLZIP_OPT_REMOVE_PATH => 'optional',
724 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
725 PCLZIP_OPT_ADD_PATH => 'optional',
726 PCLZIP_CB_PRE_EXTRACT => 'optional',
727 PCLZIP_CB_POST_EXTRACT => 'optional',
728 PCLZIP_OPT_SET_CHMOD => 'optional',
729 PCLZIP_OPT_BY_NAME => 'optional',
730 PCLZIP_OPT_BY_EREG => 'optional',
731 PCLZIP_OPT_BY_PREG => 'optional',
732 PCLZIP_OPT_BY_INDEX => 'optional',
733 PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
734 PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
735 PCLZIP_OPT_REPLACE_NEWER => 'optional'
736 ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
737 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional'
739 if ($v_result != 1) {
740 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
741 return 0;
744 // ----- Set the arguments
745 if (isset($v_options[PCLZIP_OPT_PATH])) {
746 $v_path = $v_options[PCLZIP_OPT_PATH];
748 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
749 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
751 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
752 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
754 if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
755 // ----- Check for '/' in last path char
756 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
757 $v_path .= '/';
759 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
763 // ----- Look for 2 args
764 // Here we need to support the first historic synopsis of the
765 // method.
766 else {
767 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
769 // ----- Get the first argument
770 $v_path = $v_arg_list[0];
772 // ----- Look for the optional second argument
773 if ($v_size == 2) {
774 $v_remove_path = $v_arg_list[1];
776 else if ($v_size > 2) {
777 // ----- Error log
778 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
780 // ----- Return
781 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
782 return 0;
787 // ----- Trace
788 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
790 // ----- Call the extracting fct
791 $p_list = array();
792 $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
793 $v_remove_all_path, $v_options);
794 if ($v_result < 1) {
795 unset($p_list);
796 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
797 return(0);
800 // ----- Return
801 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
802 return $p_list;
804 // --------------------------------------------------------------------------------
807 // --------------------------------------------------------------------------------
808 // Function :
809 // extractByIndex($p_index, $p_path="./", $p_remove_path="")
810 // extractByIndex($p_index, [$p_option, $p_option_value, ...])
811 // Description :
812 // This method supports two synopsis. The first one is historical.
813 // This method is doing a partial extract of the archive.
814 // The extracted files or folders are identified by their index in the
815 // archive (from 0 to n).
816 // Note that if the index identify a folder, only the folder entry is
817 // extracted, not all the files included in the archive.
818 // Parameters :
819 // $p_index : A single index (integer) or a string of indexes of files to
820 // extract. The form of the string is "0,4-6,8-12" with only numbers
821 // and '-' for range or ',' to separate ranges. No spaces or ';'
822 // are allowed.
823 // $p_path : Path where the files and directories are to be extracted
824 // $p_remove_path : First part ('root' part) of the memorized path
825 // (if any similar) to remove while extracting.
826 // Options :
827 // PCLZIP_OPT_PATH :
828 // PCLZIP_OPT_ADD_PATH :
829 // PCLZIP_OPT_REMOVE_PATH :
830 // PCLZIP_OPT_REMOVE_ALL_PATH :
831 // PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
832 // not as files.
833 // The resulting content is in a new field 'content' in the file
834 // structure.
835 // This option must be used alone (any other options are ignored).
836 // PCLZIP_CB_PRE_EXTRACT :
837 // PCLZIP_CB_POST_EXTRACT :
838 // Return Values :
839 // 0 on failure,
840 // The list of the extracted files, with a status of the action.
841 // (see PclZip::listContent() for list entry format)
842 // --------------------------------------------------------------------------------
843 //function extractByIndex($p_index, options...)
844 function extractByIndex($p_index)
846 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extractByIndex", "index='$p_index', ...");
847 $v_result=1;
849 // ----- Reset the error handler
850 $this->privErrorReset();
852 // ----- Check archive
853 if (!$this->privCheckFormat()) {
854 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
855 return(0);
858 // ----- Set default values
859 $v_options = array();
860 // $v_path = "./";
861 $v_path = '';
862 $v_remove_path = "";
863 $v_remove_all_path = false;
865 // ----- Look for variable options arguments
866 $v_size = func_num_args();
867 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
869 // ----- Default values for option
870 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
872 // ----- Look for arguments
873 if ($v_size > 1) {
874 // ----- Get the arguments
875 $v_arg_list = func_get_args();
877 // ----- Remove form the options list the first argument
878 array_shift($v_arg_list);
879 $v_size--;
881 // ----- Look for first arg
882 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
883 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
885 // ----- Parse the options
886 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
887 array (PCLZIP_OPT_PATH => 'optional',
888 PCLZIP_OPT_REMOVE_PATH => 'optional',
889 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
890 PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
891 PCLZIP_OPT_ADD_PATH => 'optional',
892 PCLZIP_CB_PRE_EXTRACT => 'optional',
893 PCLZIP_CB_POST_EXTRACT => 'optional',
894 PCLZIP_OPT_SET_CHMOD => 'optional',
895 PCLZIP_OPT_REPLACE_NEWER => 'optional'
896 ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
897 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional'
899 if ($v_result != 1) {
900 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
901 return 0;
904 // ----- Set the arguments
905 if (isset($v_options[PCLZIP_OPT_PATH])) {
906 $v_path = $v_options[PCLZIP_OPT_PATH];
908 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
909 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
911 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
912 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
914 if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
915 // ----- Check for '/' in last path char
916 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
917 $v_path .= '/';
919 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
921 if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
922 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
923 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING not set.");
925 else {
926 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING set.");
930 // ----- Look for 2 args
931 // Here we need to support the first historic synopsis of the
932 // method.
933 else {
934 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
936 // ----- Get the first argument
937 $v_path = $v_arg_list[0];
939 // ----- Look for the optional second argument
940 if ($v_size == 2) {
941 $v_remove_path = $v_arg_list[1];
943 else if ($v_size > 2) {
944 // ----- Error log
945 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
947 // ----- Return
948 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
949 return 0;
954 // ----- Trace
955 //--(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')."'");
957 // ----- Trick
958 // Here I want to reuse extractByRule(), so I need to parse the $p_index
959 // with privParseOptions()
960 $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
961 $v_options_trick = array();
962 $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
963 array (PCLZIP_OPT_BY_INDEX => 'optional' ));
964 if ($v_result != 1) {
965 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
966 return 0;
968 $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
970 // ----- Call the extracting fct
971 if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
972 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
973 return(0);
976 // ----- Return
977 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
978 return $p_list;
980 // --------------------------------------------------------------------------------
982 // --------------------------------------------------------------------------------
983 // Function :
984 // delete([$p_option, $p_option_value, ...])
985 // Description :
986 // This method removes files from the archive.
987 // If no parameters are given, then all the archive is emptied.
988 // Parameters :
989 // None or optional arguments.
990 // Options :
991 // PCLZIP_OPT_BY_INDEX :
992 // PCLZIP_OPT_BY_NAME :
993 // PCLZIP_OPT_BY_EREG :
994 // PCLZIP_OPT_BY_PREG :
995 // Return Values :
996 // 0 on failure,
997 // The list of the files which are still present in the archive.
998 // (see PclZip::listContent() for list entry format)
999 // --------------------------------------------------------------------------------
1000 function delete()
1002 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::delete", "");
1003 $v_result=1;
1005 // ----- Reset the error handler
1006 $this->privErrorReset();
1008 // ----- Check archive
1009 if (!$this->privCheckFormat()) {
1010 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1011 return(0);
1014 // ----- Set default values
1015 $v_options = array();
1017 // ----- Look for variable options arguments
1018 $v_size = func_num_args();
1019 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
1021 // ----- Look for arguments
1022 if ($v_size > 0) {
1023 // ----- Get the arguments
1024 $v_arg_list = func_get_args();
1026 // ----- Parse the options
1027 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
1028 array (PCLZIP_OPT_BY_NAME => 'optional',
1029 PCLZIP_OPT_BY_EREG => 'optional',
1030 PCLZIP_OPT_BY_PREG => 'optional',
1031 PCLZIP_OPT_BY_INDEX => 'optional' ));
1032 if ($v_result != 1) {
1033 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1034 return 0;
1038 // ----- Magic quotes trick
1039 $this->privDisableMagicQuotes();
1041 // ----- Call the delete fct
1042 $v_list = array();
1043 if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
1044 $this->privSwapBackMagicQuotes();
1045 unset($v_list);
1046 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
1047 return(0);
1050 // ----- Magic quotes trick
1051 $this->privSwapBackMagicQuotes();
1053 // ----- Return
1054 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_list);
1055 return $v_list;
1057 // --------------------------------------------------------------------------------
1059 // --------------------------------------------------------------------------------
1060 // Function : deleteByIndex()
1061 // Description :
1062 // ***** Deprecated *****
1063 // delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
1064 // --------------------------------------------------------------------------------
1065 function deleteByIndex($p_index)
1067 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::deleteByIndex", "index='$p_index'");
1069 $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
1071 // ----- Return
1072 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
1073 return $p_list;
1075 // --------------------------------------------------------------------------------
1077 // --------------------------------------------------------------------------------
1078 // Function : properties()
1079 // Description :
1080 // This method gives the properties of the archive.
1081 // The properties are :
1082 // nb : Number of files in the archive
1083 // comment : Comment associated with the archive file
1084 // status : not_exist, ok
1085 // Parameters :
1086 // None
1087 // Return Values :
1088 // 0 on failure,
1089 // An array with the archive properties.
1090 // --------------------------------------------------------------------------------
1091 function properties()
1093 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::properties", "");
1095 // ----- Reset the error handler
1096 $this->privErrorReset();
1098 // ----- Magic quotes trick
1099 $this->privDisableMagicQuotes();
1101 // ----- Check archive
1102 if (!$this->privCheckFormat()) {
1103 $this->privSwapBackMagicQuotes();
1104 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1105 return(0);
1108 // ----- Default properties
1109 $v_prop = array();
1110 $v_prop['comment'] = '';
1111 $v_prop['nb'] = 0;
1112 $v_prop['status'] = 'not_exist';
1114 // ----- Look if file exists
1115 if (@is_file($this->zipname))
1117 // ----- Open the zip file
1118 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
1119 if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
1121 $this->privSwapBackMagicQuotes();
1123 // ----- Error log
1124 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
1126 // ----- Return
1127 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), 0);
1128 return 0;
1131 // ----- Read the central directory informations
1132 $v_central_dir = array();
1133 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
1135 $this->privSwapBackMagicQuotes();
1136 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1137 return 0;
1140 // ----- Close the zip file
1141 $this->privCloseFd();
1143 // ----- Set the user attributes
1144 $v_prop['comment'] = $v_central_dir['comment'];
1145 $v_prop['nb'] = $v_central_dir['entries'];
1146 $v_prop['status'] = 'ok';
1149 // ----- Magic quotes trick
1150 $this->privSwapBackMagicQuotes();
1152 // ----- Return
1153 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_prop);
1154 return $v_prop;
1156 // --------------------------------------------------------------------------------
1158 // --------------------------------------------------------------------------------
1159 // Function : duplicate()
1160 // Description :
1161 // This method creates an archive by copying the content of an other one. If
1162 // the archive already exist, it is replaced by the new one without any warning.
1163 // Parameters :
1164 // $p_archive : The filename of a valid archive, or
1165 // a valid PclZip object.
1166 // Return Values :
1167 // 1 on success.
1168 // 0 or a negative value on error (error code).
1169 // --------------------------------------------------------------------------------
1170 function duplicate($p_archive)
1172 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::duplicate", "");
1173 $v_result = 1;
1175 // ----- Reset the error handler
1176 $this->privErrorReset();
1178 // ----- Look if the $p_archive is a PclZip object
1179 if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
1181 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is valid PclZip object '".$p_archive->zipname."'");
1183 // ----- Duplicate the archive
1184 $v_result = $this->privDuplicate($p_archive->zipname);
1187 // ----- Look if the $p_archive is a string (so a filename)
1188 else if (is_string($p_archive))
1190 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is a filename '$p_archive'");
1192 // ----- Check that $p_archive is a valid zip file
1193 // TBC : Should also check the archive format
1194 if (!is_file($p_archive)) {
1195 // ----- Error log
1196 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
1197 $v_result = PCLZIP_ERR_MISSING_FILE;
1199 else {
1200 // ----- Duplicate the archive
1201 $v_result = $this->privDuplicate($p_archive);
1205 // ----- Invalid variable
1206 else
1208 // ----- Error log
1209 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1210 $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1213 // ----- Return
1214 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1215 return $v_result;
1217 // --------------------------------------------------------------------------------
1219 // --------------------------------------------------------------------------------
1220 // Function : merge()
1221 // Description :
1222 // This method merge the $p_archive_to_add archive at the end of the current
1223 // one ($this).
1224 // If the archive ($this) does not exist, the merge becomes a duplicate.
1225 // If the $p_archive_to_add archive does not exist, the merge is a success.
1226 // Parameters :
1227 // $p_archive_to_add : It can be directly the filename of a valid zip archive,
1228 // or a PclZip object archive.
1229 // Return Values :
1230 // 1 on success,
1231 // 0 or negative values on error (see below).
1232 // --------------------------------------------------------------------------------
1233 function merge($p_archive_to_add)
1235 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::merge", "");
1236 $v_result = 1;
1238 // ----- Reset the error handler
1239 $this->privErrorReset();
1241 // ----- Check archive
1242 if (!$this->privCheckFormat()) {
1243 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1244 return(0);
1247 // ----- Look if the $p_archive_to_add is a PclZip object
1248 if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
1250 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is valid PclZip object");
1252 // ----- Merge the archive
1253 $v_result = $this->privMerge($p_archive_to_add);
1256 // ----- Look if the $p_archive_to_add is a string (so a filename)
1257 else if (is_string($p_archive_to_add))
1259 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is a filename");
1261 // ----- Create a temporary archive
1262 $v_object_archive = new PclZip($p_archive_to_add);
1264 // ----- Merge the archive
1265 $v_result = $this->privMerge($v_object_archive);
1268 // ----- Invalid variable
1269 else
1271 // ----- Error log
1272 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1273 $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1276 // ----- Return
1277 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1278 return $v_result;
1280 // --------------------------------------------------------------------------------
1284 // --------------------------------------------------------------------------------
1285 // Function : errorCode()
1286 // Description :
1287 // Parameters :
1288 // --------------------------------------------------------------------------------
1289 function errorCode()
1291 if (PCLZIP_ERROR_EXTERNAL == 1) {
1292 return(PclErrorCode());
1294 else {
1295 return($this->error_code);
1298 // --------------------------------------------------------------------------------
1300 // --------------------------------------------------------------------------------
1301 // Function : errorName()
1302 // Description :
1303 // Parameters :
1304 // --------------------------------------------------------------------------------
1305 function errorName($p_with_code=false)
1307 $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
1308 PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
1309 PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
1310 PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
1311 PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
1312 PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
1313 PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
1314 PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
1315 PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
1316 PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
1317 PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
1318 PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
1319 PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
1320 PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
1321 PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
1322 PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
1323 PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
1324 PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
1325 PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
1326 ,PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
1327 ,PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
1330 if (isset($v_name[$this->error_code])) {
1331 $v_value = $v_name[$this->error_code];
1333 else {
1334 $v_value = 'NoName';
1337 if ($p_with_code) {
1338 return($v_value.' ('.$this->error_code.')');
1340 else {
1341 return($v_value);
1344 // --------------------------------------------------------------------------------
1346 // --------------------------------------------------------------------------------
1347 // Function : errorInfo()
1348 // Description :
1349 // Parameters :
1350 // --------------------------------------------------------------------------------
1351 function errorInfo($p_full=false)
1353 if (PCLZIP_ERROR_EXTERNAL == 1) {
1354 return(PclErrorString());
1356 else {
1357 if ($p_full) {
1358 return($this->errorName(true)." : ".$this->error_string);
1360 else {
1361 return($this->error_string." [code ".$this->error_code."]");
1365 // --------------------------------------------------------------------------------
1368 // --------------------------------------------------------------------------------
1369 // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1370 // ***** *****
1371 // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
1372 // --------------------------------------------------------------------------------
1376 // --------------------------------------------------------------------------------
1377 // Function : privCheckFormat()
1378 // Description :
1379 // This method check that the archive exists and is a valid zip archive.
1380 // Several level of check exists. (futur)
1381 // Parameters :
1382 // $p_level : Level of check. Default 0.
1383 // 0 : Check the first bytes (magic codes) (default value))
1384 // 1 : 0 + Check the central directory (futur)
1385 // 2 : 1 + Check each file header (futur)
1386 // Return Values :
1387 // true on success,
1388 // false on error, the error code is set.
1389 // --------------------------------------------------------------------------------
1390 function privCheckFormat($p_level=0)
1392 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFormat", "");
1393 $v_result = true;
1395 // ----- Reset the file system cache
1396 clearstatcache();
1398 // ----- Reset the error handler
1399 $this->privErrorReset();
1401 // ----- Look if the file exits
1402 if (!is_file($this->zipname)) {
1403 // ----- Error log
1404 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
1405 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1406 return(false);
1409 // ----- Check that the file is readeable
1410 if (!is_readable($this->zipname)) {
1411 // ----- Error log
1412 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
1413 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1414 return(false);
1417 // ----- Check the magic code
1418 // TBC
1420 // ----- Check the central header
1421 // TBC
1423 // ----- Check each file header
1424 // TBC
1426 // ----- Return
1427 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1428 return $v_result;
1430 // --------------------------------------------------------------------------------
1432 // --------------------------------------------------------------------------------
1433 // Function : privParseOptions()
1434 // Description :
1435 // This internal methods reads the variable list of arguments ($p_options_list,
1436 // $p_size) and generate an array with the options and values ($v_result_list).
1437 // $v_requested_options contains the options that can be present and those that
1438 // must be present.
1439 // $v_requested_options is an array, with the option value as key, and 'optional',
1440 // or 'mandatory' as value.
1441 // Parameters :
1442 // See above.
1443 // Return Values :
1444 // 1 on success.
1445 // 0 on failure.
1446 // --------------------------------------------------------------------------------
1447 function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
1449 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privParseOptions", "");
1450 $v_result=1;
1452 // ----- Read the options
1453 $i=0;
1454 while ($i<$p_size) {
1455 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Looking for table index $i, option = '".PclZipUtilOptionText($p_options_list[$i])."(".$p_options_list[$i].")'");
1457 // ----- Check if the option is supported
1458 if (!isset($v_requested_options[$p_options_list[$i]])) {
1459 // ----- Error log
1460 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
1462 // ----- Return
1463 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1464 return PclZip::errorCode();
1467 // ----- Look for next option
1468 switch ($p_options_list[$i]) {
1469 // ----- Look for options that request a path value
1470 case PCLZIP_OPT_PATH :
1471 case PCLZIP_OPT_REMOVE_PATH :
1472 case PCLZIP_OPT_ADD_PATH :
1473 // ----- Check the number of parameters
1474 if (($i+1) >= $p_size) {
1475 // ----- Error log
1476 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1478 // ----- Return
1479 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1480 return PclZip::errorCode();
1483 // ----- Get the value
1484 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], false);
1485 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1486 $i++;
1487 break;
1489 case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
1490 // ----- Check the number of parameters
1491 if (($i+1) >= $p_size) {
1492 // ----- Error log
1493 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1495 // ----- Return
1496 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1497 return PclZip::errorCode();
1500 // ----- Get the value
1501 if ( is_string($p_options_list[$i+1])
1502 && ($p_options_list[$i+1] != '')) {
1503 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], false);
1504 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1505 $i++;
1507 else {
1508 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." set with an empty value is ignored.");
1510 break;
1512 // ----- Look for options that request an array of string for value
1513 case PCLZIP_OPT_BY_NAME :
1514 // ----- Check the number of parameters
1515 if (($i+1) >= $p_size) {
1516 // ----- Error log
1517 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1519 // ----- Return
1520 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1521 return PclZip::errorCode();
1524 // ----- Get the value
1525 if (is_string($p_options_list[$i+1])) {
1526 $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
1528 else if (is_array($p_options_list[$i+1])) {
1529 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1531 else {
1532 // ----- Error log
1533 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1535 // ----- Return
1536 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1537 return PclZip::errorCode();
1539 ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1540 $i++;
1541 break;
1543 // ----- Look for options that request an EREG or PREG expression
1544 case PCLZIP_OPT_BY_EREG :
1545 case PCLZIP_OPT_BY_PREG :
1546 //case PCLZIP_OPT_CRYPT :
1547 // ----- Check the number of parameters
1548 if (($i+1) >= $p_size) {
1549 // ----- Error log
1550 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1552 // ----- Return
1553 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1554 return PclZip::errorCode();
1557 // ----- Get the value
1558 if (is_string($p_options_list[$i+1])) {
1559 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1561 else {
1562 // ----- Error log
1563 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1565 // ----- Return
1566 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1567 return PclZip::errorCode();
1569 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1570 $i++;
1571 break;
1573 // ----- Look for options that takes a string
1574 case PCLZIP_OPT_COMMENT :
1575 case PCLZIP_OPT_ADD_COMMENT :
1576 case PCLZIP_OPT_PREPEND_COMMENT :
1577 // ----- Check the number of parameters
1578 if (($i+1) >= $p_size) {
1579 // ----- Error log
1580 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
1581 "Missing parameter value for option '"
1582 .PclZipUtilOptionText($p_options_list[$i])
1583 ."'");
1585 // ----- Return
1586 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1587 return PclZip::errorCode();
1590 // ----- Get the value
1591 if (is_string($p_options_list[$i+1])) {
1592 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1594 else {
1595 // ----- Error log
1596 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
1597 "Wrong parameter value for option '"
1598 .PclZipUtilOptionText($p_options_list[$i])
1599 ."'");
1601 // ----- Return
1602 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1603 return PclZip::errorCode();
1605 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1606 $i++;
1607 break;
1609 // ----- Look for options that request an array of index
1610 case PCLZIP_OPT_BY_INDEX :
1611 // ----- Check the number of parameters
1612 if (($i+1) >= $p_size) {
1613 // ----- Error log
1614 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1616 // ----- Return
1617 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1618 return PclZip::errorCode();
1621 // ----- Get the value
1622 $v_work_list = array();
1623 if (is_string($p_options_list[$i+1])) {
1624 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is a string '".$p_options_list[$i+1]."'");
1626 // ----- Remove spaces
1627 $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
1629 // ----- Parse items
1630 $v_work_list = explode(",", $p_options_list[$i+1]);
1632 else if (is_integer($p_options_list[$i+1])) {
1633 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an integer '".$p_options_list[$i+1]."'");
1634 $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
1636 else if (is_array($p_options_list[$i+1])) {
1637 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an array");
1638 $v_work_list = $p_options_list[$i+1];
1640 else {
1641 // ----- Error log
1642 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1644 // ----- Return
1645 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1646 return PclZip::errorCode();
1649 // ----- Reduce the index list
1650 // each index item in the list must be a couple with a start and
1651 // an end value : [0,3], [5-5], [8-10], ...
1652 // ----- Check the format of each item
1653 $v_sort_flag=false;
1654 $v_sort_value=0;
1655 for ($j=0; $j<sizeof($v_work_list); $j++) {
1656 // ----- Explode the item
1657 $v_item_list = explode("-", $v_work_list[$j]);
1658 $v_size_item_list = sizeof($v_item_list);
1660 // ----- TBC : Here we might check that each item is a
1661 // real integer ...
1663 // ----- Look for single value
1664 if ($v_size_item_list == 1) {
1665 // ----- Set the option value
1666 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1667 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
1669 elseif ($v_size_item_list == 2) {
1670 // ----- Set the option value
1671 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1672 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
1674 else {
1675 // ----- Error log
1676 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1678 // ----- Return
1679 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1680 return PclZip::errorCode();
1683 //--(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']."]");
1685 // ----- Look for list sort
1686 if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
1687 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The list should be sorted ...");
1688 $v_sort_flag=true;
1690 // ----- TBC : An automatic sort should be writen ...
1691 // ----- Error log
1692 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1694 // ----- Return
1695 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1696 return PclZip::errorCode();
1698 $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
1701 // ----- Sort the items
1702 if ($v_sort_flag) {
1703 // TBC : To Be Completed
1704 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "List sorting is not yet write ...");
1707 // ----- Next option
1708 $i++;
1709 break;
1711 // ----- Look for options that request no value
1712 case PCLZIP_OPT_REMOVE_ALL_PATH :
1713 case PCLZIP_OPT_EXTRACT_AS_STRING :
1714 case PCLZIP_OPT_NO_COMPRESSION :
1715 case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
1716 case PCLZIP_OPT_REPLACE_NEWER :
1717 case PCLZIP_OPT_STOP_ON_ERROR :
1718 $v_result_list[$p_options_list[$i]] = true;
1719 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1720 break;
1722 // ----- Look for options that request an octal value
1723 case PCLZIP_OPT_SET_CHMOD :
1724 // ----- Check the number of parameters
1725 if (($i+1) >= $p_size) {
1726 // ----- Error log
1727 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1729 // ----- Return
1730 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1731 return PclZip::errorCode();
1734 // ----- Get the value
1735 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1736 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1737 $i++;
1738 break;
1740 // ----- Look for options that request a call-back
1741 case PCLZIP_CB_PRE_EXTRACT :
1742 case PCLZIP_CB_POST_EXTRACT :
1743 case PCLZIP_CB_PRE_ADD :
1744 case PCLZIP_CB_POST_ADD :
1745 /* for futur use
1746 case PCLZIP_CB_PRE_DELETE :
1747 case PCLZIP_CB_POST_DELETE :
1748 case PCLZIP_CB_PRE_LIST :
1749 case PCLZIP_CB_POST_LIST :
1751 // ----- Check the number of parameters
1752 if (($i+1) >= $p_size) {
1753 // ----- Error log
1754 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1756 // ----- Return
1757 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1758 return PclZip::errorCode();
1761 // ----- Get the value
1762 $v_function_name = $p_options_list[$i+1];
1763 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "call-back ".PclZipUtilOptionText($p_options_list[$i])." = '".$v_function_name."'");
1765 // ----- Check that the value is a valid existing function
1766 if (!function_exists($v_function_name)) {
1767 // ----- Error log
1768 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1770 // ----- Return
1771 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1772 return PclZip::errorCode();
1775 // ----- Set the attribute
1776 $v_result_list[$p_options_list[$i]] = $v_function_name;
1777 $i++;
1778 break;
1780 default :
1781 // ----- Error log
1782 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
1783 "Unknown parameter '"
1784 .$p_options_list[$i]."'");
1786 // ----- Return
1787 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1788 return PclZip::errorCode();
1791 // ----- Next options
1792 $i++;
1795 // ----- Look for mandatory options
1796 if ($v_requested_options !== false) {
1797 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
1798 // ----- Look for mandatory option
1799 if ($v_requested_options[$key] == 'mandatory') {
1800 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
1801 // ----- Look if present
1802 if (!isset($v_result_list[$key])) {
1803 // ----- Error log
1804 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
1806 // ----- Return
1807 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1808 return PclZip::errorCode();
1814 // ----- Return
1815 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1816 return $v_result;
1818 // --------------------------------------------------------------------------------
1820 // --------------------------------------------------------------------------------
1821 // Function : privFileDescrParseAtt()
1822 // Description :
1823 // Parameters :
1824 // Return Values :
1825 // 1 on success.
1826 // 0 on failure.
1827 // --------------------------------------------------------------------------------
1828 function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
1830 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrParseAtt", "");
1831 $v_result=1;
1833 // ----- For each file in the list check the attributes
1834 foreach ($p_file_list as $v_key => $v_value) {
1836 // ----- Check if the option is supported
1837 if (!isset($v_requested_options[$v_key])) {
1838 // ----- Error log
1839 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
1841 // ----- Return
1842 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1843 return PclZip::errorCode();
1846 // ----- Look for attribute
1847 switch ($v_key) {
1848 case PCLZIP_ATT_FILE_NAME :
1849 if (!is_string($v_value)) {
1850 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1851 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1852 return PclZip::errorCode();
1855 $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
1856 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
1858 if ($p_filedescr['filename'] == '') {
1859 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
1860 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1861 return PclZip::errorCode();
1864 break;
1866 case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
1867 if (!is_string($v_value)) {
1868 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1869 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1870 return PclZip::errorCode();
1873 $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
1874 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
1876 if ($p_filedescr['new_short_name'] == '') {
1877 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
1878 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1879 return PclZip::errorCode();
1881 break;
1883 case PCLZIP_ATT_FILE_NEW_FULL_NAME :
1884 if (!is_string($v_value)) {
1885 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1886 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1887 return PclZip::errorCode();
1890 $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
1891 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
1893 if ($p_filedescr['new_full_name'] == '') {
1894 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
1895 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1896 return PclZip::errorCode();
1898 break;
1900 default :
1901 // ----- Error log
1902 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
1903 "Unknown parameter '".$v_key."'");
1905 // ----- Return
1906 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1907 return PclZip::errorCode();
1910 // ----- Look for mandatory options
1911 if ($v_requested_options !== false) {
1912 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
1913 // ----- Look for mandatory option
1914 if ($v_requested_options[$key] == 'mandatory') {
1915 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
1916 // ----- Look if present
1917 if (!isset($p_file_list[$key])) {
1918 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
1919 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1920 return PclZip::errorCode();
1926 // end foreach
1929 // ----- Return
1930 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1931 return $v_result;
1933 // --------------------------------------------------------------------------------
1935 // --------------------------------------------------------------------------------
1936 // Function : privFileDescrExpand()
1937 // Description :
1938 // Parameters :
1939 // Return Values :
1940 // 1 on success.
1941 // 0 on failure.
1942 // --------------------------------------------------------------------------------
1943 function privFileDescrExpand(&$p_filedescr_list, &$p_options)
1945 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrExpand", "");
1946 $v_result=1;
1948 // ----- Create a result list
1949 $v_result_list = array();
1951 // ----- Look each entry
1952 for ($i=0; $i<sizeof($p_filedescr_list); $i++) {
1953 // ----- Get filedescr
1954 $v_descr = $p_filedescr_list[$i];
1956 // ----- Reduce the filename
1957 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filedescr before reduction :'".$v_descr['filename']."'");
1958 $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename']);
1959 $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
1960 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filedescr after reduction :'".$v_descr['filename']."'");
1962 // ----- Get type of descr
1963 if (!file_exists($v_descr['filename'])) {
1964 // ----- Error log
1965 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_descr['filename']."' does not exists");
1966 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$v_descr['filename']."' does not exists");
1968 // ----- Return
1969 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1970 return PclZip::errorCode();
1972 if (@is_file($v_descr['filename'])) {
1973 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a file");
1974 $v_descr['type'] = 'file';
1976 else if (@is_dir($v_descr['filename'])) {
1977 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a folder");
1978 $v_descr['type'] = 'folder';
1980 else if (@is_link($v_descr['filename'])) {
1981 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Unsupported file type : link");
1982 // skip
1983 continue;
1985 else {
1986 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Unsupported file type : unknown type");
1987 // skip
1988 continue;
1991 // ----- Calculate the stored filename
1992 $this->privCalculateStoredFilename($v_descr, $p_options);
1994 // ----- Add the descriptor in result list
1995 $v_result_list[sizeof($v_result_list)] = $v_descr;
1997 // ----- Look for folder
1998 if ($v_descr['type'] == 'folder') {
1999 // ----- List of items in folder
2000 $v_dirlist_descr = array();
2001 $v_dirlist_nb = 0;
2002 if ($v_folder_handler = @opendir($v_descr['filename'])) {
2003 while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
2004 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for '".$v_item_handler."' in the directory");
2006 // ----- Skip '.' and '..'
2007 if (($v_item_handler == '.') || ($v_item_handler == '..')) {
2008 continue;
2011 // ----- Compose the full filename
2012 $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
2014 // ----- Look for different stored filename
2015 // Because the name of the folder was changed, the name of the
2016 // files/sub-folders also change
2017 if ($v_descr['stored_filename'] != $v_descr['filename']) {
2018 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
2021 $v_dirlist_nb++;
2024 else {
2025 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to open dir '".$v_descr['filename']."' in read mode. Skipped.");
2026 // TBC : unable to open folder in read mode
2029 // ----- Expand each element of the list
2030 if ($v_dirlist_nb != 0) {
2031 // ----- Expand
2032 if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
2033 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2034 return $v_result;
2037 // ----- Concat the resulting list
2038 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Merging result list (size '".sizeof($v_result_list)."') with dirlist (size '".sizeof($v_dirlist_descr)."')");
2039 $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
2040 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "merged result list is size '".sizeof($v_result_list)."'");
2042 else {
2043 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Nothing in this folder to expand.");
2046 // ----- Free local array
2047 unset($v_dirlist_descr);
2051 // ----- Get the result list
2052 $p_filedescr_list = $v_result_list;
2054 // ----- Return
2055 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2056 return $v_result;
2058 // --------------------------------------------------------------------------------
2060 // --------------------------------------------------------------------------------
2061 // Function : privCreate()
2062 // Description :
2063 // Parameters :
2064 // Return Values :
2065 // --------------------------------------------------------------------------------
2066 function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
2068 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCreate", "list");
2069 $v_result=1;
2070 $v_list_detail = array();
2072 // ----- Magic quotes trick
2073 $this->privDisableMagicQuotes();
2075 // ----- Open the file in write mode
2076 if (($v_result = $this->privOpenFd('wb')) != 1)
2078 // ----- Return
2079 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2080 return $v_result;
2083 // ----- Add the list of files
2084 $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
2086 // ----- Close
2087 $this->privCloseFd();
2089 // ----- Magic quotes trick
2090 $this->privSwapBackMagicQuotes();
2092 // ----- Return
2093 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2094 return $v_result;
2096 // --------------------------------------------------------------------------------
2098 // --------------------------------------------------------------------------------
2099 // Function : privAdd()
2100 // Description :
2101 // Parameters :
2102 // Return Values :
2103 // --------------------------------------------------------------------------------
2104 function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
2106 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAdd", "list");
2107 $v_result=1;
2108 $v_list_detail = array();
2110 // ----- Look if the archive exists or is empty
2111 if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
2113 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, or is empty, create it.");
2115 // ----- Do a create
2116 $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
2118 // ----- Return
2119 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2120 return $v_result;
2122 // ----- Magic quotes trick
2123 $this->privDisableMagicQuotes();
2125 // ----- Open the zip file
2126 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
2127 if (($v_result=$this->privOpenFd('rb')) != 1)
2129 // ----- Magic quotes trick
2130 $this->privSwapBackMagicQuotes();
2132 // ----- Return
2133 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2134 return $v_result;
2137 // ----- Read the central directory informations
2138 $v_central_dir = array();
2139 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
2141 $this->privCloseFd();
2142 $this->privSwapBackMagicQuotes();
2143 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2144 return $v_result;
2147 // ----- Go to beginning of File
2148 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
2149 @rewind($this->zip_fd);
2150 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
2152 // ----- Creates a temporay file
2153 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
2155 // ----- Open the temporary file in write mode
2156 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
2157 if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
2159 $this->privCloseFd();
2160 $this->privSwapBackMagicQuotes();
2162 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
2164 // ----- Return
2165 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2166 return PclZip::errorCode();
2169 // ----- Copy the files from the archive to the temporary file
2170 // TBC : Here I should better append the file and go back to erase the central dir
2171 $v_size = $v_central_dir['offset'];
2172 while ($v_size != 0)
2174 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2175 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
2176 $v_buffer = fread($this->zip_fd, $v_read_size);
2177 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
2178 $v_size -= $v_read_size;
2181 // ----- Swap the file descriptor
2182 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
2183 // the following methods on the temporary fil and not the real archive
2184 $v_swap = $this->zip_fd;
2185 $this->zip_fd = $v_zip_temp_fd;
2186 $v_zip_temp_fd = $v_swap;
2188 // ----- Add the files
2189 $v_header_list = array();
2190 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2192 fclose($v_zip_temp_fd);
2193 $this->privCloseFd();
2194 @unlink($v_zip_temp_name);
2195 $this->privSwapBackMagicQuotes();
2197 // ----- Return
2198 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2199 return $v_result;
2202 // ----- Store the offset of the central dir
2203 $v_offset = @ftell($this->zip_fd);
2204 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
2206 // ----- Copy the block of file headers from the old archive
2207 $v_size = $v_central_dir['size'];
2208 while ($v_size != 0)
2210 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2211 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
2212 $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
2213 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
2214 $v_size -= $v_read_size;
2217 // ----- Create the Central Dir files header
2218 for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++)
2220 // ----- Create the file header
2221 if ($v_header_list[$i]['status'] == 'ok') {
2222 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2223 fclose($v_zip_temp_fd);
2224 $this->privCloseFd();
2225 @unlink($v_zip_temp_name);
2226 $this->privSwapBackMagicQuotes();
2228 // ----- Return
2229 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2230 return $v_result;
2232 $v_count++;
2235 // ----- Transform the header to a 'usable' info
2236 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2239 // ----- Zip file comment
2240 $v_comment = $v_central_dir['comment'];
2241 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2242 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2244 if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
2245 $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
2247 if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
2248 $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
2251 // ----- Calculate the size of the central header
2252 $v_size = @ftell($this->zip_fd)-$v_offset;
2254 // ----- Create the central dir footer
2255 if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
2257 // ----- Reset the file list
2258 unset($v_header_list);
2259 $this->privSwapBackMagicQuotes();
2261 // ----- Return
2262 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2263 return $v_result;
2266 // ----- Swap back the file descriptor
2267 $v_swap = $this->zip_fd;
2268 $this->zip_fd = $v_zip_temp_fd;
2269 $v_zip_temp_fd = $v_swap;
2271 // ----- Close
2272 $this->privCloseFd();
2274 // ----- Close the temporary file
2275 @fclose($v_zip_temp_fd);
2277 // ----- Magic quotes trick
2278 $this->privSwapBackMagicQuotes();
2280 // ----- Delete the zip file
2281 // TBC : I should test the result ...
2282 @unlink($this->zipname);
2284 // ----- Rename the temporary file
2285 // TBC : I should test the result ...
2286 //@rename($v_zip_temp_name, $this->zipname);
2287 PclZipUtilRename($v_zip_temp_name, $this->zipname);
2289 // ----- Return
2290 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2291 return $v_result;
2293 // --------------------------------------------------------------------------------
2295 // --------------------------------------------------------------------------------
2296 // Function : privOpenFd()
2297 // Description :
2298 // Parameters :
2299 // --------------------------------------------------------------------------------
2300 function privOpenFd($p_mode)
2302 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privOpenFd", 'mode='.$p_mode);
2303 $v_result=1;
2305 // ----- Look if already open
2306 if ($this->zip_fd != 0)
2308 // ----- Error log
2309 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
2311 // ----- Return
2312 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2313 return PclZip::errorCode();
2316 // ----- Open the zip file
2317 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Open file in '.$p_mode.' mode');
2318 if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
2320 // ----- Error log
2321 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
2323 // ----- Return
2324 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2325 return PclZip::errorCode();
2328 // ----- Return
2329 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2330 return $v_result;
2332 // --------------------------------------------------------------------------------
2334 // --------------------------------------------------------------------------------
2335 // Function : privCloseFd()
2336 // Description :
2337 // Parameters :
2338 // --------------------------------------------------------------------------------
2339 function privCloseFd()
2341 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCloseFd", "");
2342 $v_result=1;
2344 if ($this->zip_fd != 0)
2345 @fclose($this->zip_fd);
2346 $this->zip_fd = 0;
2348 // ----- Return
2349 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2350 return $v_result;
2352 // --------------------------------------------------------------------------------
2354 // --------------------------------------------------------------------------------
2355 // Function : privAddList()
2356 // Description :
2357 // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
2358 // different from the real path of the file. This is usefull if you want to have PclTar
2359 // running in any directory, and memorize relative path from an other directory.
2360 // Parameters :
2361 // $p_list : An array containing the file or directory names to add in the tar
2362 // $p_result_list : list of added files with their properties (specially the status field)
2363 // $p_add_dir : Path to add in the filename path archived
2364 // $p_remove_dir : Path to remove in the filename path archived
2365 // Return Values :
2366 // --------------------------------------------------------------------------------
2367 // function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
2368 function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
2370 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddList", "list");
2371 $v_result=1;
2373 // ----- Add the files
2374 $v_header_list = array();
2375 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2377 // ----- Return
2378 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2379 return $v_result;
2382 // ----- Store the offset of the central dir
2383 $v_offset = @ftell($this->zip_fd);
2385 // ----- Create the Central Dir files header
2386 for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
2388 // ----- Create the file header
2389 if ($v_header_list[$i]['status'] == 'ok') {
2390 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2391 // ----- Return
2392 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2393 return $v_result;
2395 $v_count++;
2398 // ----- Transform the header to a 'usable' info
2399 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2402 // ----- Zip file comment
2403 $v_comment = '';
2404 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2405 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2408 // ----- Calculate the size of the central header
2409 $v_size = @ftell($this->zip_fd)-$v_offset;
2411 // ----- Create the central dir footer
2412 if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
2414 // ----- Reset the file list
2415 unset($v_header_list);
2417 // ----- Return
2418 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2419 return $v_result;
2422 // ----- Return
2423 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2424 return $v_result;
2426 // --------------------------------------------------------------------------------
2428 // --------------------------------------------------------------------------------
2429 // Function : privAddFileList()
2430 // Description :
2431 // Parameters :
2432 // $p_filedescr_list : An array containing the file description
2433 // or directory names to add in the zip
2434 // $p_result_list : list of added files with their properties (specially the status field)
2435 // Return Values :
2436 // --------------------------------------------------------------------------------
2437 function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
2439 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFileList", "filedescr_list");
2440 $v_result=1;
2441 $v_header = array();
2443 // ----- Recuperate the current number of elt in list
2444 $v_nb = sizeof($p_result_list);
2445 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Before add, list have ".$v_nb." elements");
2447 // ----- Loop on the files
2448 for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++) {
2449 // ----- Format the filename
2450 $p_filedescr_list[$j]['filename']
2451 = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
2453 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for file '".$p_filedescr_list[$j]['filename']."'");
2455 // ----- Skip empty file names
2456 // TBC : Can this be possible ? not checked in DescrParseAtt ?
2457 if ($p_filedescr_list[$j]['filename'] == "") {
2458 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Skip empty filename");
2459 continue;
2462 // ----- Check the filename
2463 if (!file_exists($p_filedescr_list[$j]['filename'])) {
2464 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$p_filedescr_list[$j]['filename']."' does not exists");
2465 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$p_filedescr_list[$j]['filename']."' does not exists");
2466 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2467 return PclZip::errorCode();
2470 // ----- Look if it is a file or a dir with no all path remove option
2471 if ( (is_file($p_filedescr_list[$j]['filename']))
2472 || ( is_dir($p_filedescr_list[$j]['filename'])
2473 && ( !isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])
2474 || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
2476 // ----- Add the file
2477 $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
2478 $p_options);
2479 if ($v_result != 1) {
2480 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2481 return $v_result;
2484 // ----- Store the file infos
2485 $p_result_list[$v_nb++] = $v_header;
2488 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "After add, list have ".$v_nb." elements");
2490 // ----- Return
2491 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2492 return $v_result;
2494 // --------------------------------------------------------------------------------
2496 // --------------------------------------------------------------------------------
2497 // Function : privAddFile()
2498 // Description :
2499 // Parameters :
2500 // Return Values :
2501 // --------------------------------------------------------------------------------
2502 function privAddFile($p_filedescr, &$p_header, &$p_options)
2504 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFile", "filename='".$p_filedescr['filename']."'");
2505 $v_result=1;
2507 // ----- Working variable
2508 $p_filename = $p_filedescr['filename'];
2510 // TBC : Already done in the fileAtt check ... ?
2511 if ($p_filename == "") {
2512 // ----- Error log
2513 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
2515 // ----- Return
2516 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2517 return PclZip::errorCode();
2520 // ----- Look for a stored different filename
2521 if (isset($p_filedescr['stored_filename'])) {
2522 $v_stored_filename = $p_filedescr['stored_filename'];
2523 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is NOT the same "'.$v_stored_filename.'"');
2525 else {
2526 $v_stored_filename = $p_filedescr['stored_filename'];
2527 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is the same');
2530 // ----- Set the file properties
2531 clearstatcache();
2532 $p_header['version'] = 20;
2533 $p_header['version_extracted'] = 10;
2534 $p_header['flag'] = 0;
2535 $p_header['compression'] = 0;
2536 $p_header['mtime'] = filemtime($p_filename);
2537 $p_header['crc'] = 0;
2538 $p_header['compressed_size'] = 0;
2539 $p_header['size'] = filesize($p_filename);
2540 $p_header['filename_len'] = strlen($p_filename);
2541 $p_header['extra_len'] = 0;
2542 $p_header['comment_len'] = 0;
2543 $p_header['disk'] = 0;
2544 $p_header['internal'] = 0;
2545 // $p_header['external'] = (is_file($p_filename)?0xFE49FFE0:0x41FF0010);
2546 $p_header['external'] = (is_file($p_filename)?0x00000000:0x00000010);
2547 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header external extension '".sprintf("0x%X",$p_header['external'])."'");
2548 $p_header['offset'] = 0;
2549 $p_header['filename'] = $p_filename;
2550 $p_header['stored_filename'] = $v_stored_filename;
2551 $p_header['extra'] = '';
2552 $p_header['comment'] = '';
2553 $p_header['status'] = 'ok';
2554 $p_header['index'] = -1;
2556 // ----- Look for pre-add callback
2557 if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
2558 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_ADD]."()') is defined for the extraction");
2560 // ----- Generate a local information
2561 $v_local_header = array();
2562 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2564 // ----- Call the callback
2565 // Here I do not use call_user_func() because I need to send a reference to the
2566 // header.
2567 eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
2568 if ($v_result == 0) {
2569 // ----- Change the file status
2570 $p_header['status'] = "skipped";
2571 $v_result = 1;
2574 // ----- Update the informations
2575 // Only some fields can be modified
2576 if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
2577 $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
2578 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New stored filename is '".$p_header['stored_filename']."'");
2582 // ----- Look for empty stored filename
2583 if ($p_header['stored_filename'] == "") {
2584 $p_header['status'] = "filtered";
2587 // ----- Check the path length
2588 if (strlen($p_header['stored_filename']) > 0xFF) {
2589 $p_header['status'] = 'filename_too_long';
2592 // ----- Look if no error, or file not skipped
2593 if ($p_header['status'] == 'ok') {
2595 // ----- Look for a file
2596 if (is_file($p_filename))
2598 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a file");
2599 // ----- Open the source file
2600 if (($v_file = @fopen($p_filename, "rb")) == 0) {
2601 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
2602 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2603 return PclZip::errorCode();
2606 if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
2607 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be compressed");
2608 // ----- Read the file content
2609 $v_content_compressed = @fread($v_file, $p_header['size']);
2611 // ----- Calculate the CRC
2612 $p_header['crc'] = @crc32($v_content_compressed);
2614 // ----- Set header parameters
2615 $p_header['compressed_size'] = $p_header['size'];
2616 $p_header['compression'] = 0;
2618 else {
2619 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will be compressed");
2620 // ----- Read the file content
2621 $v_content = @fread($v_file, $p_header['size']);
2623 // ----- Calculate the CRC
2624 $p_header['crc'] = @crc32($v_content);
2626 // ----- Compress the file
2627 $v_content_compressed = @gzdeflate($v_content);
2629 // ----- Set header parameters
2630 $p_header['compressed_size'] = strlen($v_content_compressed);
2631 $p_header['compression'] = 8;
2634 // ----- Look for encryption
2636 if ((isset($p_options[PCLZIP_OPT_CRYPT]))
2637 && ($p_options[PCLZIP_OPT_CRYPT] != "")) {
2638 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File need to be crypted ....");
2640 // Should be a random header
2641 $v_header = 'xxxxxxxxxxxx';
2642 $v_content_compressed = PclZipUtilZipEncrypt($v_content_compressed,
2643 $p_header['compressed_size'],
2644 $v_header,
2645 $p_header['crc'],
2646 "test");
2648 $p_header['compressed_size'] += 12;
2649 $p_header['flag'] = 1;
2651 // ----- Add the header to the data
2652 $v_content_compressed = $v_header.$v_content_compressed;
2653 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size after header : ".strlen($v_content_compressed)."");
2657 // ----- Call the header generation
2658 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2659 @fclose($v_file);
2660 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2661 return $v_result;
2664 // ----- Write the compressed (or not) content
2665 @fwrite($this->zip_fd,
2666 $v_content_compressed, $p_header['compressed_size']);
2668 // ----- Close the file
2669 @fclose($v_file);
2672 // ----- Look for a directory
2673 else {
2674 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a folder");
2675 // ----- Look for directory last '/'
2676 if (@substr($p_header['stored_filename'], -1) != '/') {
2677 $p_header['stored_filename'] .= '/';
2680 // ----- Set the file properties
2681 $p_header['size'] = 0;
2682 //$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
2683 $p_header['external'] = 0x00000010; // Value for a folder : to be checked
2685 // ----- Call the header generation
2686 if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
2688 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2689 return $v_result;
2694 // ----- Look for post-add callback
2695 if (isset($p_options[PCLZIP_CB_POST_ADD])) {
2696 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_ADD]."()') is defined for the extraction");
2698 // ----- Generate a local information
2699 $v_local_header = array();
2700 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2702 // ----- Call the callback
2703 // Here I do not use call_user_func() because I need to send a reference to the
2704 // header.
2705 eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
2706 if ($v_result == 0) {
2707 // ----- Ignored
2708 $v_result = 1;
2711 // ----- Update the informations
2712 // Nothing can be modified
2715 // ----- Return
2716 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2717 return $v_result;
2719 // --------------------------------------------------------------------------------
2721 // --------------------------------------------------------------------------------
2722 // Function : privCalculateStoredFilename()
2723 // Description :
2724 // Based on file descriptor properties and global options, this method
2725 // calculate the filename that will be stored in the archive.
2726 // Parameters :
2727 // Return Values :
2728 // --------------------------------------------------------------------------------
2729 function privCalculateStoredFilename(&$p_filedescr, &$p_options)
2731 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCalculateStoredFilename", "filename='".$p_filedescr['filename']."'");
2732 $v_result=1;
2734 // ----- Working variables
2735 $p_filename = $p_filedescr['filename'];
2736 if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
2737 $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
2739 else {
2740 $p_add_dir = '';
2742 if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
2743 $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
2745 else {
2746 $p_remove_dir = '';
2748 if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
2749 $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
2751 else {
2752 $p_remove_all_dir = 0;
2755 // ----- Look for full name change
2756 if (isset($p_filedescr['new_full_name'])) {
2757 $v_stored_filename = $p_filedescr['new_full_name'];
2758 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing full name of '".$p_filename."' for '".$v_stored_filename."'");
2761 // ----- Look for path and/or short name change
2762 else {
2764 // ----- Look for short name change
2765 if (isset($p_filedescr['new_short_name'])) {
2766 $v_path_info = pathinfo($p_filename);
2767 $v_dir = '';
2768 if ($v_path_info['dirname'] != '') {
2769 $v_dir = $v_path_info['dirname'].'/';
2771 $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
2772 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing short name of '".$p_filename."' for '".$v_stored_filename."'");
2774 else {
2775 // ----- Calculate the stored filename
2776 $v_stored_filename = $p_filename;
2779 // ----- Look for all path to remove
2780 if ($p_remove_all_dir) {
2781 $v_stored_filename = basename($p_filename);
2782 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove all path selected change '".$p_filename."' for '".$v_stored_filename."'");
2784 // ----- Look for partial path remove
2785 else if ($p_remove_dir != "") {
2786 if (substr($p_remove_dir, -1) != '/')
2787 $p_remove_dir .= "/";
2789 if ( (substr($p_filename, 0, 2) == "./")
2790 || (substr($p_remove_dir, 0, 2) == "./")) {
2792 if ( (substr($p_filename, 0, 2) == "./")
2793 && (substr($p_remove_dir, 0, 2) != "./")) {
2794 $p_remove_dir = "./".$p_remove_dir;
2796 if ( (substr($p_filename, 0, 2) != "./")
2797 && (substr($p_remove_dir, 0, 2) == "./")) {
2798 $p_remove_dir = substr($p_remove_dir, 2);
2802 $v_compare = PclZipUtilPathInclusion($p_remove_dir,
2803 $v_stored_filename);
2804 if ($v_compare > 0) {
2805 if ($v_compare == 2) {
2806 $v_stored_filename = "";
2807 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Path to remove is the current folder");
2809 else {
2810 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove path '$p_remove_dir' in file '$v_stored_filename'");
2811 $v_stored_filename = substr($v_stored_filename,
2812 strlen($p_remove_dir));
2813 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Result is '$v_stored_filename'");
2817 // ----- Look for path to add
2818 if ($p_add_dir != "") {
2819 if (substr($p_add_dir, -1) == "/")
2820 $v_stored_filename = $p_add_dir.$v_stored_filename;
2821 else
2822 $v_stored_filename = $p_add_dir."/".$v_stored_filename;
2823 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
2827 // ----- Filename (reduce the path of stored name)
2828 $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
2829 $p_filedescr['stored_filename'] = $v_stored_filename;
2830 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Stored filename will be '".$p_filedescr['stored_filename']."', strlen ".strlen($p_filedescr['stored_filename']));
2832 // ----- Return
2833 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2834 return $v_result;
2836 // --------------------------------------------------------------------------------
2838 // --------------------------------------------------------------------------------
2839 // Function : privWriteFileHeader()
2840 // Description :
2841 // Parameters :
2842 // Return Values :
2843 // --------------------------------------------------------------------------------
2844 function privWriteFileHeader(&$p_header)
2846 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
2847 $v_result=1;
2849 // ----- Store the offset position of the file
2850 $p_header['offset'] = ftell($this->zip_fd);
2851 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'File offset of the header :'.$p_header['offset']);
2853 // ----- Transform UNIX mtime to DOS format mdate/mtime
2854 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
2855 $v_date = getdate($p_header['mtime']);
2856 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
2857 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
2859 // ----- Packed data
2860 $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
2861 $p_header['version_extracted'], $p_header['flag'],
2862 $p_header['compression'], $v_mtime, $v_mdate,
2863 $p_header['crc'], $p_header['compressed_size'],
2864 $p_header['size'],
2865 strlen($p_header['stored_filename']),
2866 $p_header['extra_len']);
2868 // ----- Write the first 148 bytes of the header in the archive
2869 fputs($this->zip_fd, $v_binary_data, 30);
2871 // ----- Write the variable fields
2872 if (strlen($p_header['stored_filename']) != 0)
2874 fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
2876 if ($p_header['extra_len'] != 0)
2878 fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
2881 // ----- Return
2882 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2883 return $v_result;
2885 // --------------------------------------------------------------------------------
2887 // --------------------------------------------------------------------------------
2888 // Function : privWriteCentralFileHeader()
2889 // Description :
2890 // Parameters :
2891 // Return Values :
2892 // --------------------------------------------------------------------------------
2893 function privWriteCentralFileHeader(&$p_header)
2895 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
2896 $v_result=1;
2898 // TBC
2899 //for(reset($p_header); $key = key($p_header); next($p_header)) {
2900 // //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "header[$key] = ".$p_header[$key]);
2903 // ----- Transform UNIX mtime to DOS format mdate/mtime
2904 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
2905 $v_date = getdate($p_header['mtime']);
2906 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
2907 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
2909 // ----- Packed data
2910 $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
2911 $p_header['version'], $p_header['version_extracted'],
2912 $p_header['flag'], $p_header['compression'],
2913 $v_mtime, $v_mdate, $p_header['crc'],
2914 $p_header['compressed_size'], $p_header['size'],
2915 strlen($p_header['stored_filename']),
2916 $p_header['extra_len'], $p_header['comment_len'],
2917 $p_header['disk'], $p_header['internal'],
2918 $p_header['external'], $p_header['offset']);
2920 // ----- Write the 42 bytes of the header in the zip file
2921 fputs($this->zip_fd, $v_binary_data, 46);
2923 // ----- Write the variable fields
2924 if (strlen($p_header['stored_filename']) != 0)
2926 fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
2928 if ($p_header['extra_len'] != 0)
2930 fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
2932 if ($p_header['comment_len'] != 0)
2934 fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
2937 // ----- Return
2938 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2939 return $v_result;
2941 // --------------------------------------------------------------------------------
2943 // --------------------------------------------------------------------------------
2944 // Function : privWriteCentralHeader()
2945 // Description :
2946 // Parameters :
2947 // Return Values :
2948 // --------------------------------------------------------------------------------
2949 function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
2951 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralHeader", 'nb_entries='.$p_nb_entries.', size='.$p_size.', offset='.$p_offset.', comment="'.$p_comment.'"');
2952 $v_result=1;
2954 // ----- Packed data
2955 $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
2956 $p_nb_entries, $p_size,
2957 $p_offset, strlen($p_comment));
2959 // ----- Write the 22 bytes of the header in the zip file
2960 fputs($this->zip_fd, $v_binary_data, 22);
2962 // ----- Write the variable fields
2963 if (strlen($p_comment) != 0)
2965 fputs($this->zip_fd, $p_comment, strlen($p_comment));
2968 // ----- Return
2969 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2970 return $v_result;
2972 // --------------------------------------------------------------------------------
2974 // --------------------------------------------------------------------------------
2975 // Function : privList()
2976 // Description :
2977 // Parameters :
2978 // Return Values :
2979 // --------------------------------------------------------------------------------
2980 function privList(&$p_list)
2982 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privList", "list");
2983 $v_result=1;
2985 // ----- Magic quotes trick
2986 $this->privDisableMagicQuotes();
2988 // ----- Open the zip file
2989 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
2990 if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
2992 // ----- Magic quotes trick
2993 $this->privSwapBackMagicQuotes();
2995 // ----- Error log
2996 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
2998 // ----- Return
2999 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3000 return PclZip::errorCode();
3003 // ----- Read the central directory informations
3004 $v_central_dir = array();
3005 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3007 $this->privSwapBackMagicQuotes();
3008 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3009 return $v_result;
3012 // ----- Go to beginning of Central Dir
3013 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Offset : ".$v_central_dir['offset']."'");
3014 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
3015 @rewind($this->zip_fd);
3016 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
3017 if (@fseek($this->zip_fd, $v_central_dir['offset']))
3019 $this->privSwapBackMagicQuotes();
3021 // ----- Error log
3022 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3024 // ----- Return
3025 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3026 return PclZip::errorCode();
3028 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
3030 // ----- Read each entry
3031 for ($i=0; $i<$v_central_dir['entries']; $i++)
3033 // ----- Read the file header
3034 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3036 $this->privSwapBackMagicQuotes();
3037 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3038 return $v_result;
3040 $v_header['index'] = $i;
3042 // ----- Get the only interesting attributes
3043 $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
3044 unset($v_header);
3047 // ----- Close the zip file
3048 $this->privCloseFd();
3050 // ----- Magic quotes trick
3051 $this->privSwapBackMagicQuotes();
3053 // ----- Return
3054 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3055 return $v_result;
3057 // --------------------------------------------------------------------------------
3059 // --------------------------------------------------------------------------------
3060 // Function : privConvertHeader2FileInfo()
3061 // Description :
3062 // This function takes the file informations from the central directory
3063 // entries and extract the interesting parameters that will be given back.
3064 // The resulting file infos are set in the array $p_info
3065 // $p_info['filename'] : Filename with full path. Given by user (add),
3066 // extracted in the filesystem (extract).
3067 // $p_info['stored_filename'] : Stored filename in the archive.
3068 // $p_info['size'] = Size of the file.
3069 // $p_info['compressed_size'] = Compressed size of the file.
3070 // $p_info['mtime'] = Last modification date of the file.
3071 // $p_info['comment'] = Comment associated with the file.
3072 // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
3073 // $p_info['status'] = status of the action on the file.
3074 // Parameters :
3075 // Return Values :
3076 // --------------------------------------------------------------------------------
3077 function privConvertHeader2FileInfo($p_header, &$p_info)
3079 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privConvertHeader2FileInfo", "Filename='".$p_header['filename']."'");
3080 $v_result=1;
3082 // ----- Get the interesting attributes
3083 $p_info['filename'] = $p_header['filename'];
3084 $p_info['stored_filename'] = $p_header['stored_filename'];
3085 $p_info['size'] = $p_header['size'];
3086 $p_info['compressed_size'] = $p_header['compressed_size'];
3087 $p_info['mtime'] = $p_header['mtime'];
3088 $p_info['comment'] = $p_header['comment'];
3089 $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
3090 $p_info['index'] = $p_header['index'];
3091 $p_info['status'] = $p_header['status'];
3093 // ----- Return
3094 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3095 return $v_result;
3097 // --------------------------------------------------------------------------------
3099 // --------------------------------------------------------------------------------
3100 // Function : privExtractByRule()
3101 // Description :
3102 // Extract a file or directory depending of rules (by index, by name, ...)
3103 // Parameters :
3104 // $p_file_list : An array where will be placed the properties of each
3105 // extracted file
3106 // $p_path : Path to add while writing the extracted files
3107 // $p_remove_path : Path to remove (from the file memorized path) while writing the
3108 // extracted files. If the path does not match the file path,
3109 // the file is extracted with its memorized path.
3110 // $p_remove_path does not apply to 'list' mode.
3111 // $p_path and $p_remove_path are commulative.
3112 // Return Values :
3113 // 1 on success,0 or less on error (see error code list)
3114 // --------------------------------------------------------------------------------
3115 function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3117 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privExtractByRule", "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
3118 $v_result=1;
3120 // ----- Magic quotes trick
3121 $this->privDisableMagicQuotes();
3123 // ----- Check the path
3124 if ( ($p_path == "")
3125 || ( (substr($p_path, 0, 1) != "/")
3126 && (substr($p_path, 0, 3) != "../")
3127 && (substr($p_path,1,2)!=":/")))
3128 $p_path = "./".$p_path;
3130 // ----- Reduce the path last (and duplicated) '/'
3131 if (($p_path != "./") && ($p_path != "/"))
3133 // ----- Look for the path end '/'
3134 while (substr($p_path, -1) == "/")
3136 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
3137 $p_path = substr($p_path, 0, strlen($p_path)-1);
3138 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
3142 // ----- Look for path to remove format (should end by /)
3143 if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
3145 $p_remove_path .= '/';
3147 $p_remove_path_size = strlen($p_remove_path);
3149 // ----- Open the zip file
3150 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
3151 if (($v_result = $this->privOpenFd('rb')) != 1)
3153 $this->privSwapBackMagicQuotes();
3154 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3155 return $v_result;
3158 // ----- Read the central directory informations
3159 $v_central_dir = array();
3160 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3162 // ----- Close the zip file
3163 $this->privCloseFd();
3164 $this->privSwapBackMagicQuotes();
3166 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3167 return $v_result;
3170 // ----- Start at beginning of Central Dir
3171 $v_pos_entry = $v_central_dir['offset'];
3173 // ----- Read each entry
3174 $j_start = 0;
3175 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
3177 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry : '$i'");
3179 // ----- Read next Central dir entry
3180 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position before rewind : ".ftell($this->zip_fd)."'");
3181 @rewind($this->zip_fd);
3182 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position after rewind : ".ftell($this->zip_fd)."'");
3183 if (@fseek($this->zip_fd, $v_pos_entry))
3185 // ----- Close the zip file
3186 $this->privCloseFd();
3187 $this->privSwapBackMagicQuotes();
3189 // ----- Error log
3190 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3192 // ----- Return
3193 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3194 return PclZip::errorCode();
3196 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position after fseek : ".ftell($this->zip_fd)."'");
3198 // ----- Read the file header
3199 $v_header = array();
3200 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3202 // ----- Close the zip file
3203 $this->privCloseFd();
3204 $this->privSwapBackMagicQuotes();
3206 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3207 return $v_result;
3210 // ----- Store the index
3211 $v_header['index'] = $i;
3213 // ----- Store the file position
3214 $v_pos_entry = ftell($this->zip_fd);
3216 // ----- Look for the specific extract rules
3217 $v_extract = false;
3219 // ----- Look for extract by name rule
3220 if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
3221 && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
3222 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
3224 // ----- Look if the filename is in the list
3225 for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
3226 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
3228 // ----- Look for a directory
3229 if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
3230 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
3232 // ----- Look if the directory is in the filename path
3233 if ( (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
3234 && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
3235 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
3236 $v_extract = true;
3239 // ----- Look for a filename
3240 elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
3241 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
3242 $v_extract = true;
3247 // ----- Look for extract by ereg rule
3248 else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
3249 && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
3250 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
3252 if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
3253 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
3254 $v_extract = true;
3258 // ----- Look for extract by preg rule
3259 else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
3260 && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
3261 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
3263 if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
3264 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
3265 $v_extract = true;
3269 // ----- Look for extract by index rule
3270 else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
3271 && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
3272 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
3274 // ----- Look if the index is in the list
3275 for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
3276 //--(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']."]");
3278 if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
3279 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
3280 $v_extract = true;
3282 if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
3283 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
3284 $j_start = $j+1;
3287 if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
3288 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
3289 break;
3294 // ----- Look for no rule, which means extract all the archive
3295 else {
3296 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with no rule (extract all)");
3297 $v_extract = true;
3300 // ----- Check compression method
3301 if ( ($v_extract)
3302 && ( ($v_header['compression'] != 8)
3303 && ($v_header['compression'] != 0))) {
3304 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported compression method (".$v_header['compression'].")");
3305 $v_header['status'] = 'unsupported_compression';
3307 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3308 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3309 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3310 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3312 $this->privSwapBackMagicQuotes();
3314 PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
3315 "Filename '".$v_header['stored_filename']."' is "
3316 ."compressed by an unsupported compression "
3317 ."method (".$v_header['compression'].") ");
3319 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3320 return PclZip::errorCode();
3324 // ----- Check encrypted files
3325 if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
3326 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported file encryption");
3327 $v_header['status'] = 'unsupported_encryption';
3329 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3330 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3331 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3332 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3334 $this->privSwapBackMagicQuotes();
3336 PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
3337 "Unsupported encryption for "
3338 ." filename '".$v_header['stored_filename']
3339 ."'");
3341 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3342 return PclZip::errorCode();
3346 // ----- Look for real extraction
3347 if (($v_extract) && ($v_header['status'] != 'ok')) {
3348 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "No need for extract");
3349 $v_result = $this->privConvertHeader2FileInfo($v_header,
3350 $p_file_list[$v_nb_extracted++]);
3351 if ($v_result != 1) {
3352 $this->privCloseFd();
3353 $this->privSwapBackMagicQuotes();
3354 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3355 return $v_result;
3358 $v_extract = false;
3361 // ----- Look for real extraction
3362 if ($v_extract)
3364 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file '".$v_header['filename']."', index '$i'");
3366 // ----- Go to the file position
3367 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
3368 @rewind($this->zip_fd);
3369 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
3370 if (@fseek($this->zip_fd, $v_header['offset']))
3372 // ----- Close the zip file
3373 $this->privCloseFd();
3375 $this->privSwapBackMagicQuotes();
3377 // ----- Error log
3378 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3380 // ----- Return
3381 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3382 return PclZip::errorCode();
3384 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
3386 // ----- Look for extraction as string
3387 if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
3389 // ----- Extracting the file
3390 $v_result1 = $this->privExtractFileAsString($v_header, $v_string);
3391 if ($v_result1 < 1) {
3392 $this->privCloseFd();
3393 $this->privSwapBackMagicQuotes();
3394 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
3395 return $v_result1;
3398 // ----- Get the only interesting attributes
3399 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
3401 // ----- Close the zip file
3402 $this->privCloseFd();
3403 $this->privSwapBackMagicQuotes();
3405 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3406 return $v_result;
3409 // ----- Set the file content
3410 $p_file_list[$v_nb_extracted]['content'] = $v_string;
3412 // ----- Next extracted file
3413 $v_nb_extracted++;
3415 // ----- Look for user callback abort
3416 if ($v_result1 == 2) {
3417 break;
3420 // ----- Look for extraction in standard output
3421 elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
3422 && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
3423 // ----- Extracting the file in standard output
3424 $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
3425 if ($v_result1 < 1) {
3426 $this->privCloseFd();
3427 $this->privSwapBackMagicQuotes();
3428 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
3429 return $v_result1;
3432 // ----- Get the only interesting attributes
3433 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
3434 $this->privCloseFd();
3435 $this->privSwapBackMagicQuotes();
3436 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3437 return $v_result;
3440 // ----- Look for user callback abort
3441 if ($v_result1 == 2) {
3442 break;
3445 // ----- Look for normal extraction
3446 else {
3447 // ----- Extracting the file
3448 $v_result1 = $this->privExtractFile($v_header,
3449 $p_path, $p_remove_path,
3450 $p_remove_all_path,
3451 $p_options);
3452 if ($v_result1 < 1) {
3453 $this->privCloseFd();
3454 $this->privSwapBackMagicQuotes();
3455 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
3456 return $v_result1;
3459 // ----- Get the only interesting attributes
3460 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
3462 // ----- Close the zip file
3463 $this->privCloseFd();
3464 $this->privSwapBackMagicQuotes();
3466 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3467 return $v_result;
3470 // ----- Look for user callback abort
3471 if ($v_result1 == 2) {
3472 break;
3478 // ----- Close the zip file
3479 $this->privCloseFd();
3480 $this->privSwapBackMagicQuotes();
3482 // ----- Return
3483 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3484 return $v_result;
3486 // --------------------------------------------------------------------------------
3488 // --------------------------------------------------------------------------------
3489 // Function : privExtractFile()
3490 // Description :
3491 // Parameters :
3492 // Return Values :
3494 // 1 : ... ?
3495 // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
3496 // --------------------------------------------------------------------------------
3497 function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3499 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFile', "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
3500 $v_result=1;
3502 // ----- Read the file header
3503 if (($v_result = $this->privReadFileHeader($v_header)) != 1)
3505 // ----- Return
3506 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3507 return $v_result;
3510 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
3512 // ----- Check that the file header is coherent with $p_entry info
3513 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
3514 // TBC
3517 // ----- Look for all path to remove
3518 if ($p_remove_all_path == true) {
3519 // ----- Look for folder entry that not need to be extracted
3520 if (($p_entry['external']&0x00000010)==0x00000010) {
3521 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The entry is a folder : need to be filtered");
3523 $p_entry['status'] = "filtered";
3525 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3526 return $v_result;
3529 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "All path is removed");
3530 // ----- Get the basename of the path
3531 $p_entry['filename'] = basename($p_entry['filename']);
3534 // ----- Look for path to remove
3535 else if ($p_remove_path != "")
3537 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look for some path to remove");
3538 if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
3540 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The folder is the same as the removed path '".$p_entry['filename']."'");
3542 // ----- Change the file status
3543 $p_entry['status'] = "filtered";
3545 // ----- Return
3546 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3547 return $v_result;
3550 $p_remove_path_size = strlen($p_remove_path);
3551 if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
3553 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file '".$p_entry['filename']."'");
3555 // ----- Remove the path
3556 $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
3558 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Resulting file is '".$p_entry['filename']."'");
3562 // ----- Add the path
3563 if ($p_path != '') {
3564 $p_entry['filename'] = $p_path."/".$p_entry['filename'];
3567 // ----- Check a base_dir_restriction
3568 if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
3569 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Check the extract directory restriction");
3570 $v_inclusion
3571 = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
3572 $p_entry['filename']);
3573 if ($v_inclusion == 0) {
3574 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_EXTRACT_DIR_RESTRICTION is selected, file is outside restriction");
3576 PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
3577 "Filename is outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
3579 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3580 return PclZip::errorCode();
3584 // ----- Look for pre-extract callback
3585 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
3586 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
3588 // ----- Generate a local information
3589 $v_local_header = array();
3590 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3592 // ----- Call the callback
3593 // Here I do not use call_user_func() because I need to send a reference to the
3594 // header.
3595 eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
3596 if ($v_result == 0) {
3597 // ----- Change the file status
3598 $p_entry['status'] = "skipped";
3599 $v_result = 1;
3602 // ----- Look for abort result
3603 if ($v_result == 2) {
3604 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
3605 // ----- This status is internal and will be changed in 'skipped'
3606 $p_entry['status'] = "aborted";
3607 $v_result = PCLZIP_ERR_USER_ABORTED;
3610 // ----- Update the informations
3611 // Only some fields can be modified
3612 $p_entry['filename'] = $v_local_header['filename'];
3613 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
3616 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
3618 // ----- Look if extraction should be done
3619 if ($p_entry['status'] == 'ok') {
3621 // ----- Look for specific actions while the file exist
3622 if (file_exists($p_entry['filename']))
3624 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$p_entry['filename']."' already exists");
3626 // ----- Look if file is a directory
3627 if (is_dir($p_entry['filename']))
3629 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is a directory");
3631 // ----- Change the file status
3632 $p_entry['status'] = "already_a_directory";
3634 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3635 // For historical reason first PclZip implementation does not stop
3636 // when this kind of error occurs.
3637 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3638 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3639 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3641 PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
3642 "Filename '".$p_entry['filename']."' is "
3643 ."already used by an existing directory");
3645 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3646 return PclZip::errorCode();
3649 // ----- Look if file is write protected
3650 else if (!is_writeable($p_entry['filename']))
3652 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is write protected");
3654 // ----- Change the file status
3655 $p_entry['status'] = "write_protected";
3657 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3658 // For historical reason first PclZip implementation does not stop
3659 // when this kind of error occurs.
3660 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3661 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3662 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3664 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
3665 "Filename '".$p_entry['filename']."' exists "
3666 ."and is write protected");
3668 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3669 return PclZip::errorCode();
3673 // ----- Look if the extracted file is older
3674 else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
3676 //--(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']).")");
3677 // ----- Change the file status
3678 if ( (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
3679 && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
3680 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_REPLACE_NEWER is selected, file will be replaced");
3682 else {
3683 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be replaced");
3684 $p_entry['status'] = "newer_exist";
3686 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3687 // For historical reason first PclZip implementation does not stop
3688 // when this kind of error occurs.
3689 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3690 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3691 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3693 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
3694 "Newer version of '".$p_entry['filename']."' exists "
3695 ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
3697 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3698 return PclZip::errorCode();
3702 else {
3703 //--(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']).")");
3707 // ----- Check the directory availability and create it if necessary
3708 else {
3709 if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
3710 $v_dir_to_check = $p_entry['filename'];
3711 else if (!strstr($p_entry['filename'], "/"))
3712 $v_dir_to_check = "";
3713 else
3714 $v_dir_to_check = dirname($p_entry['filename']);
3716 if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
3717 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '".$p_entry['filename']."'");
3719 // ----- Change the file status
3720 $p_entry['status'] = "path_creation_fail";
3722 // ----- Return
3723 ////--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3724 //return $v_result;
3725 $v_result = 1;
3730 // ----- Look if extraction should be done
3731 if ($p_entry['status'] == 'ok') {
3733 // ----- Do the extraction (if not a folder)
3734 if (!(($p_entry['external']&0x00000010)==0x00000010))
3736 // ----- Look for not compressed file
3737 if ($p_entry['compression'] == 0) {
3738 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
3740 // ----- Opening destination file
3741 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
3743 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
3745 // ----- Change the file status
3746 $p_entry['status'] = "write_error";
3748 // ----- Return
3749 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3750 return $v_result;
3753 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read '".$p_entry['size']."' bytes");
3755 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3756 $v_size = $p_entry['compressed_size'];
3757 while ($v_size != 0)
3759 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3760 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read $v_read_size bytes");
3761 $v_buffer = @fread($this->zip_fd, $v_read_size);
3762 /* Try to speed up the code
3763 $v_binary_data = pack('a'.$v_read_size, $v_buffer);
3764 @fwrite($v_dest_file, $v_binary_data, $v_read_size);
3766 @fwrite($v_dest_file, $v_buffer, $v_read_size);
3767 $v_size -= $v_read_size;
3770 // ----- Closing the destination file
3771 fclose($v_dest_file);
3773 // ----- Change the file mtime
3774 touch($p_entry['filename'], $p_entry['mtime']);
3778 else {
3779 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file (Compression method ".$p_entry['compression'].")");
3780 // ----- TBC
3781 // Need to be finished
3782 if (($p_entry['flag'] & 1) == 1) {
3783 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File is encrypted");
3785 // ----- Read the encryption header
3786 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read 12 encryption header bytes");
3787 $v_encryption_header = @fread($this->zip_fd, 12);
3789 // ----- Read the encrypted & compressed file in a buffer
3790 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read '".($p_entry['compressed_size']-12)."' compressed & encrypted bytes");
3791 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']-12);
3793 // ----- Decrypt the buffer
3794 $this->privDecrypt($v_encryption_header, $v_buffer,
3795 $p_entry['compressed_size']-12, $p_entry['crc']);
3796 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Buffer is '".$v_buffer."'");
3799 else {
3800 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read '".$p_entry['compressed_size']."' compressed bytes");
3801 // ----- Read the compressed file in a buffer (one shot)
3802 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3805 // ----- Decompress the file
3806 $v_file_content = @gzinflate($v_buffer);
3807 unset($v_buffer);
3808 if ($v_file_content === FALSE) {
3809 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to inflate compressed file");
3811 // ----- Change the file status
3812 // TBC
3813 $p_entry['status'] = "error";
3815 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3816 return $v_result;
3819 // ----- Opening destination file
3820 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
3821 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
3823 // ----- Change the file status
3824 $p_entry['status'] = "write_error";
3826 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3827 return $v_result;
3830 // ----- Write the uncompressed data
3831 @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
3832 unset($v_file_content);
3834 // ----- Closing the destination file
3835 @fclose($v_dest_file);
3837 // ----- Change the file mtime
3838 @touch($p_entry['filename'], $p_entry['mtime']);
3841 // ----- Look for chmod option
3842 if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
3843 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "chmod option activated '".$p_options[PCLZIP_OPT_SET_CHMOD]."'");
3845 // ----- Change the mode of the file
3846 @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
3849 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
3853 // ----- Change abort status
3854 if ($p_entry['status'] == "aborted") {
3855 $p_entry['status'] = "skipped";
3858 // ----- Look for post-extract callback
3859 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
3860 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
3862 // ----- Generate a local information
3863 $v_local_header = array();
3864 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3866 // ----- Call the callback
3867 // Here I do not use call_user_func() because I need to send a reference to the
3868 // header.
3869 eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
3871 // ----- Look for abort result
3872 if ($v_result == 2) {
3873 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
3874 $v_result = PCLZIP_ERR_USER_ABORTED;
3878 // ----- Return
3879 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3880 return $v_result;
3882 // --------------------------------------------------------------------------------
3884 // --------------------------------------------------------------------------------
3885 // Function : privExtractFileInOutput()
3886 // Description :
3887 // Parameters :
3888 // Return Values :
3889 // --------------------------------------------------------------------------------
3890 function privExtractFileInOutput(&$p_entry, &$p_options)
3892 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileInOutput', "");
3893 $v_result=1;
3895 // ----- Read the file header
3896 if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
3897 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3898 return $v_result;
3901 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
3903 // ----- Check that the file header is coherent with $p_entry info
3904 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
3905 // TBC
3908 // ----- Look for pre-extract callback
3909 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
3910 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
3912 // ----- Generate a local information
3913 $v_local_header = array();
3914 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3916 // ----- Call the callback
3917 // Here I do not use call_user_func() because I need to send a reference to the
3918 // header.
3919 eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
3920 if ($v_result == 0) {
3921 // ----- Change the file status
3922 $p_entry['status'] = "skipped";
3923 $v_result = 1;
3926 // ----- Look for abort result
3927 if ($v_result == 2) {
3928 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
3929 // ----- This status is internal and will be changed in 'skipped'
3930 $p_entry['status'] = "aborted";
3931 $v_result = PCLZIP_ERR_USER_ABORTED;
3934 // ----- Update the informations
3935 // Only some fields can be modified
3936 $p_entry['filename'] = $v_local_header['filename'];
3937 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
3940 // ----- Trace
3941 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
3943 // ----- Look if extraction should be done
3944 if ($p_entry['status'] == 'ok') {
3946 // ----- Do the extraction (if not a folder)
3947 if (!(($p_entry['external']&0x00000010)==0x00000010)) {
3948 // ----- Look for not compressed file
3949 if ($p_entry['compressed_size'] == $p_entry['size']) {
3950 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
3951 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
3953 // ----- Read the file in a buffer (one shot)
3954 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3956 // ----- Send the file to the output
3957 echo $v_buffer;
3958 unset($v_buffer);
3960 else {
3961 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file");
3962 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Reading '".$p_entry['size']."' bytes");
3964 // ----- Read the compressed file in a buffer (one shot)
3965 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3967 // ----- Decompress the file
3968 $v_file_content = gzinflate($v_buffer);
3969 unset($v_buffer);
3971 // ----- Send the file to the output
3972 echo $v_file_content;
3973 unset($v_file_content);
3975 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
3979 // ----- Change abort status
3980 if ($p_entry['status'] == "aborted") {
3981 $p_entry['status'] = "skipped";
3984 // ----- Look for post-extract callback
3985 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
3986 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
3988 // ----- Generate a local information
3989 $v_local_header = array();
3990 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3992 // ----- Call the callback
3993 // Here I do not use call_user_func() because I need to send a reference to the
3994 // header.
3995 eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
3997 // ----- Look for abort result
3998 if ($v_result == 2) {
3999 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
4000 $v_result = PCLZIP_ERR_USER_ABORTED;
4004 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4005 return $v_result;
4007 // --------------------------------------------------------------------------------
4009 // --------------------------------------------------------------------------------
4010 // Function : privExtractFileAsString()
4011 // Description :
4012 // Parameters :
4013 // Return Values :
4014 // --------------------------------------------------------------------------------
4015 function privExtractFileAsString(&$p_entry, &$p_string)
4017 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileAsString', "p_entry['filename']='".$p_entry['filename']."'");
4018 $v_result=1;
4020 // ----- Read the file header
4021 $v_header = array();
4022 if (($v_result = $this->privReadFileHeader($v_header)) != 1)
4024 // ----- Return
4025 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4026 return $v_result;
4029 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
4031 // ----- Check that the file header is coherent with $p_entry info
4032 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
4033 // TBC
4036 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file in string (with path) '".$p_entry['filename']."', size '$v_header[size]'");
4038 // ----- Do the extraction (if not a folder)
4039 if (!(($p_entry['external']&0x00000010)==0x00000010))
4041 // ----- Look for not compressed file
4042 // if ($p_entry['compressed_size'] == $p_entry['size'])
4043 if ($p_entry['compression'] == 0) {
4044 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
4045 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
4047 // ----- Reading the file
4048 $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
4050 else {
4051 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file (compression method '".$p_entry['compression']."')");
4053 // ----- Reading the file
4054 $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
4056 // ----- Decompress the file
4057 if (($p_string = @gzinflate($v_data)) === FALSE) {
4058 // TBC
4062 // ----- Trace
4063 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
4065 else {
4066 // TBC : error : can not extract a folder in a string
4069 // ----- Return
4070 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4071 return $v_result;
4073 // --------------------------------------------------------------------------------
4075 // --------------------------------------------------------------------------------
4076 // Function : privReadFileHeader()
4077 // Description :
4078 // Parameters :
4079 // Return Values :
4080 // --------------------------------------------------------------------------------
4081 function privReadFileHeader(&$p_header)
4083 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadFileHeader", "");
4084 $v_result=1;
4086 // ----- Read the 4 bytes signature
4087 $v_binary_data = @fread($this->zip_fd, 4);
4088 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
4089 $v_data = unpack('Vid', $v_binary_data);
4090 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
4092 // ----- Check signature
4093 if ($v_data['id'] != 0x04034b50)
4095 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid File header");
4097 // ----- Error log
4098 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
4100 // ----- Return
4101 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4102 return PclZip::errorCode();
4105 // ----- Read the first 42 bytes of the header
4106 $v_binary_data = fread($this->zip_fd, 26);
4108 // ----- Look for invalid block size
4109 if (strlen($v_binary_data) != 26)
4111 $p_header['filename'] = "";
4112 $p_header['status'] = "invalid_header";
4113 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
4115 // ----- Error log
4116 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
4118 // ----- Return
4119 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4120 return PclZip::errorCode();
4123 // ----- Extract the values
4124 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header : '".$v_binary_data."'");
4125 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header (Hex) : '".bin2hex($v_binary_data)."'");
4126 $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
4128 // ----- Get filename
4129 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "File name length : ".$v_data['filename_len']);
4130 $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
4131 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename : \''.$p_header['filename'].'\'');
4133 // ----- Get extra_fields
4134 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extra field length : ".$v_data['extra_len']);
4135 if ($v_data['extra_len'] != 0) {
4136 $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
4138 else {
4139 $p_header['extra'] = '';
4141 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Extra field : \''.bin2hex($p_header['extra']).'\'');
4143 // ----- Extract properties
4144 $p_header['version_extracted'] = $v_data['version'];
4145 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : ('.$p_header['version_extracted'].') \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
4146 $p_header['compression'] = $v_data['compression'];
4147 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compression method : \''.$p_header['compression'].'\'');
4148 $p_header['size'] = $v_data['size'];
4149 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_header['size'].'\'');
4150 $p_header['compressed_size'] = $v_data['compressed_size'];
4151 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
4152 $p_header['crc'] = $v_data['crc'];
4153 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
4154 $p_header['flag'] = $v_data['flag'];
4155 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag : \''.$p_header['flag'].'\'');
4156 $p_header['filename_len'] = $v_data['filename_len'];
4157 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename_len : \''.$p_header['filename_len'].'\'');
4159 // ----- Recuperate date in UNIX format
4160 $p_header['mdate'] = $v_data['mdate'];
4161 $p_header['mtime'] = $v_data['mtime'];
4162 if ($p_header['mdate'] && $p_header['mtime'])
4164 // ----- Extract time
4165 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
4166 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
4167 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
4169 // ----- Extract date
4170 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
4171 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
4172 $v_day = $p_header['mdate'] & 0x001F;
4174 // ----- Get UNIX date format
4175 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
4177 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
4179 else
4181 $p_header['mtime'] = time();
4182 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
4185 // TBC
4186 //for(reset($v_data); $key = key($v_data); next($v_data)) {
4187 // //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Attribut[$key] = ".$v_data[$key]);
4190 // ----- Set the stored filename
4191 $p_header['stored_filename'] = $p_header['filename'];
4193 // ----- Set the status field
4194 $p_header['status'] = "ok";
4196 // ----- Return
4197 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4198 return $v_result;
4200 // --------------------------------------------------------------------------------
4202 // --------------------------------------------------------------------------------
4203 // Function : privReadCentralFileHeader()
4204 // Description :
4205 // Parameters :
4206 // Return Values :
4207 // --------------------------------------------------------------------------------
4208 function privReadCentralFileHeader(&$p_header)
4210 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadCentralFileHeader", "");
4211 $v_result=1;
4213 // ----- Read the 4 bytes signature
4214 $v_binary_data = @fread($this->zip_fd, 4);
4215 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
4216 $v_data = unpack('Vid', $v_binary_data);
4217 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
4219 // ----- Check signature
4220 if ($v_data['id'] != 0x02014b50)
4222 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid Central Dir File signature");
4224 // ----- Error log
4225 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
4227 // ----- Return
4228 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4229 return PclZip::errorCode();
4232 // ----- Read the first 42 bytes of the header
4233 $v_binary_data = fread($this->zip_fd, 42);
4235 // ----- Look for invalid block size
4236 if (strlen($v_binary_data) != 42)
4238 $p_header['filename'] = "";
4239 $p_header['status'] = "invalid_header";
4240 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
4242 // ----- Error log
4243 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
4245 // ----- Return
4246 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4247 return PclZip::errorCode();
4250 // ----- Extract the values
4251 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header : '".$v_binary_data."'");
4252 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header (Hex) : '".bin2hex($v_binary_data)."'");
4253 $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);
4255 // ----- Get filename
4256 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File name length : ".$p_header['filename_len']);
4257 if ($p_header['filename_len'] != 0)
4258 $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
4259 else
4260 $p_header['filename'] = '';
4261 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Filename : \''.$p_header['filename'].'\'');
4263 // ----- Get extra
4264 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Extra length : ".$p_header['extra_len']);
4265 if ($p_header['extra_len'] != 0)
4266 $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
4267 else
4268 $p_header['extra'] = '';
4269 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Extra : \''.$p_header['extra'].'\'');
4271 // ----- Get comment
4272 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Comment length : ".$p_header['comment_len']);
4273 if ($p_header['comment_len'] != 0)
4274 $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
4275 else
4276 $p_header['comment'] = '';
4277 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Comment : \''.$p_header['comment'].'\'');
4279 // ----- Extract properties
4280 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version : \''.($p_header['version']/10).'.'.($p_header['version']%10).'\'');
4281 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
4282 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Size : \''.$p_header['size'].'\'');
4283 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
4284 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
4285 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Flag : \''.$p_header['flag'].'\'');
4286 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Offset : \''.$p_header['offset'].'\'');
4288 // ----- Recuperate date in UNIX format
4289 if ($p_header['mdate'] && $p_header['mtime'])
4291 // ----- Extract time
4292 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
4293 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
4294 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
4296 // ----- Extract date
4297 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
4298 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
4299 $v_day = $p_header['mdate'] & 0x001F;
4301 // ----- Get UNIX date format
4302 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
4304 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
4306 else
4308 $p_header['mtime'] = time();
4309 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
4312 // ----- Set the stored filename
4313 $p_header['stored_filename'] = $p_header['filename'];
4315 // ----- Set default status to ok
4316 $p_header['status'] = 'ok';
4318 // ----- Look if it is a directory
4319 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Internal (Hex) : '".sprintf("Ox%04X", $p_header['internal'])."'");
4320 //--(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').')');
4321 if (substr($p_header['filename'], -1) == '/') {
4322 //$p_header['external'] = 0x41FF0010;
4323 $p_header['external'] = 0x00000010;
4324 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Force folder external : \''.sprintf("Ox%04X", $p_header['external']).'\'');
4327 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Header of filename : \''.$p_header['filename'].'\'');
4329 // ----- Return
4330 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4331 return $v_result;
4333 // --------------------------------------------------------------------------------
4335 // --------------------------------------------------------------------------------
4336 // Function : privCheckFileHeaders()
4337 // Description :
4338 // Parameters :
4339 // Return Values :
4340 // 1 on success,
4341 // 0 on error;
4342 // --------------------------------------------------------------------------------
4343 function privCheckFileHeaders(&$p_local_header, &$p_central_header)
4345 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFileHeaders", "");
4346 $v_result=1;
4348 // ----- Check the static values
4349 // TBC
4350 if ($p_local_header['filename'] != $p_central_header['filename']) {
4351 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename" : TBC To Be Completed');
4353 if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
4354 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "version_extracted" : TBC To Be Completed');
4356 if ($p_local_header['flag'] != $p_central_header['flag']) {
4357 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "flag" : TBC To Be Completed');
4359 if ($p_local_header['compression'] != $p_central_header['compression']) {
4360 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "compression" : TBC To Be Completed');
4362 if ($p_local_header['mtime'] != $p_central_header['mtime']) {
4363 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "mtime" : TBC To Be Completed');
4365 if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
4366 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename_len" : TBC To Be Completed');
4369 // ----- Look for flag bit 3
4370 if (($p_local_header['flag'] & 8) == 8) {
4371 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Purpose bit flag bit 3 set !');
4372 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'File size, compression size and crc found in central header');
4373 $p_local_header['size'] = $p_central_header['size'];
4374 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_local_header['size'].'\'');
4375 $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
4376 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_local_header['compressed_size'].'\'');
4377 $p_local_header['crc'] = $p_central_header['crc'];
4378 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_local_header['crc']).'\'');
4381 // ----- Return
4382 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4383 return $v_result;
4385 // --------------------------------------------------------------------------------
4387 // --------------------------------------------------------------------------------
4388 // Function : privReadEndCentralDir()
4389 // Description :
4390 // Parameters :
4391 // Return Values :
4392 // --------------------------------------------------------------------------------
4393 function privReadEndCentralDir(&$p_central_dir)
4395 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadEndCentralDir", "");
4396 $v_result=1;
4398 // ----- Go to the end of the zip file
4399 $v_size = filesize($this->zipname);
4400 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size of the file :$v_size");
4401 @fseek($this->zip_fd, $v_size);
4402 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position at end of zip file : \''.ftell($this->zip_fd).'\'');
4403 if (@ftell($this->zip_fd) != $v_size)
4405 // ----- Error log
4406 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
4408 // ----- Return
4409 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4410 return PclZip::errorCode();
4413 // ----- First try : look if this is an archive with no commentaries (most of the time)
4414 // in this case the end of central dir is at 22 bytes of the file end
4415 $v_found = 0;
4416 if ($v_size > 26) {
4417 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Look for central dir with no comment');
4418 @fseek($this->zip_fd, $v_size-22);
4419 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after min central position : \''.ftell($this->zip_fd).'\'');
4420 if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
4422 // ----- Error log
4423 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
4425 // ----- Return
4426 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4427 return PclZip::errorCode();
4430 // ----- Read for bytes
4431 $v_binary_data = @fread($this->zip_fd, 4);
4432 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
4433 $v_data = @unpack('Vid', $v_binary_data);
4434 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
4436 // ----- Check signature
4437 if ($v_data['id'] == 0x06054b50) {
4438 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found central dir at the default position.");
4439 $v_found = 1;
4442 $v_pos = ftell($this->zip_fd);
4445 // ----- Go back to the maximum possible size of the Central Dir End Record
4446 if (!$v_found) {
4447 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Start extended search of end central dir');
4448 $v_maximum_size = 65557; // 0xFFFF + 22;
4449 if ($v_maximum_size > $v_size)
4450 $v_maximum_size = $v_size;
4451 @fseek($this->zip_fd, $v_size-$v_maximum_size);
4452 if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
4454 // ----- Error log
4455 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
4457 // ----- Return
4458 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4459 return PclZip::errorCode();
4461 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after max central position : \''.ftell($this->zip_fd).'\'');
4463 // ----- Read byte per byte in order to find the signature
4464 $v_pos = ftell($this->zip_fd);
4465 $v_bytes = 0x00000000;
4466 while ($v_pos < $v_size)
4468 // ----- Read a byte
4469 $v_byte = @fread($this->zip_fd, 1);
4471 // ----- Add the byte
4472 $v_bytes = ($v_bytes << 8) | Ord($v_byte);
4474 // ----- Compare the bytes
4475 if ($v_bytes == 0x504b0506)
4477 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Found End Central Dir signature at position : \''.ftell($this->zip_fd).'\'');
4478 $v_pos++;
4479 break;
4482 $v_pos++;
4485 // ----- Look if not found end of central dir
4486 if ($v_pos == $v_size)
4488 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to find End of Central Dir Record signature");
4490 // ----- Error log
4491 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
4493 // ----- Return
4494 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4495 return PclZip::errorCode();
4499 // ----- Read the first 18 bytes of the header
4500 $v_binary_data = fread($this->zip_fd, 18);
4502 // ----- Look for invalid block size
4503 if (strlen($v_binary_data) != 18)
4505 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
4507 // ----- Error log
4508 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
4510 // ----- Return
4511 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4512 return PclZip::errorCode();
4515 // ----- Extract the values
4516 ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record : '".$v_binary_data."'");
4517 ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record (Hex) : '".bin2hex($v_binary_data)."'");
4518 $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
4520 // ----- Check the global size
4521 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Comment length : ".$v_data['comment_size']);
4522 if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
4523 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The central dir is not at the end of the archive. Some trailing bytes exists after the archive.");
4525 // ----- Removed in release 2.2 see readme file
4526 // The check of the file size is a little too strict.
4527 // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
4528 // While decrypted, zip has training 0 bytes
4529 if (0) {
4530 // ----- Error log
4531 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
4532 'The central dir is not at the end of the archive.'
4533 .' Some trailing bytes exists after the archive.');
4535 // ----- Return
4536 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4537 return PclZip::errorCode();
4541 // ----- Get comment
4542 if ($v_data['comment_size'] != 0)
4543 $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
4544 else
4545 $p_central_dir['comment'] = '';
4546 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment : \''.$p_central_dir['comment'].'\'');
4548 $p_central_dir['entries'] = $v_data['entries'];
4549 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries : \''.$p_central_dir['entries'].'\'');
4550 $p_central_dir['disk_entries'] = $v_data['disk_entries'];
4551 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries for this disk : \''.$p_central_dir['disk_entries'].'\'');
4552 $p_central_dir['offset'] = $v_data['offset'];
4553 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Offset of Central Dir : \''.$p_central_dir['offset'].'\'');
4554 $p_central_dir['size'] = $v_data['size'];
4555 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size of Central Dir : \''.$p_central_dir['size'].'\'');
4556 $p_central_dir['disk'] = $v_data['disk'];
4557 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Disk number : \''.$p_central_dir['disk'].'\'');
4558 $p_central_dir['disk_start'] = $v_data['disk_start'];
4559 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Start disk number : \''.$p_central_dir['disk_start'].'\'');
4561 // TBC
4562 //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
4563 // //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "central_dir[$key] = ".$p_central_dir[$key]);
4566 // ----- Return
4567 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4568 return $v_result;
4570 // --------------------------------------------------------------------------------
4572 // --------------------------------------------------------------------------------
4573 // Function : privDeleteByRule()
4574 // Description :
4575 // Parameters :
4576 // Return Values :
4577 // --------------------------------------------------------------------------------
4578 function privDeleteByRule(&$p_result_list, &$p_options)
4580 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDeleteByRule", "");
4581 $v_result=1;
4582 $v_list_detail = array();
4584 // ----- Open the zip file
4585 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
4586 if (($v_result=$this->privOpenFd('rb')) != 1)
4588 // ----- Return
4589 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4590 return $v_result;
4593 // ----- Read the central directory informations
4594 $v_central_dir = array();
4595 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
4597 $this->privCloseFd();
4598 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4599 return $v_result;
4602 // ----- Go to beginning of File
4603 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
4604 @rewind($this->zip_fd);
4605 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
4607 // ----- Scan all the files
4608 // ----- Start at beginning of Central Dir
4609 $v_pos_entry = $v_central_dir['offset'];
4610 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
4611 @rewind($this->zip_fd);
4612 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
4613 if (@fseek($this->zip_fd, $v_pos_entry))
4615 // ----- Close the zip file
4616 $this->privCloseFd();
4618 // ----- Error log
4619 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
4621 // ----- Return
4622 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4623 return PclZip::errorCode();
4625 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
4627 // ----- Read each entry
4628 $v_header_list = array();
4629 $j_start = 0;
4630 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
4632 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry (index '$i')");
4634 // ----- Read the file header
4635 $v_header_list[$v_nb_extracted] = array();
4636 if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
4638 // ----- Close the zip file
4639 $this->privCloseFd();
4641 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4642 return $v_result;
4645 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename (index '$i') : '".$v_header_list[$v_nb_extracted]['stored_filename']."'");
4647 // ----- Store the index
4648 $v_header_list[$v_nb_extracted]['index'] = $i;
4650 // ----- Look for the specific extract rules
4651 $v_found = false;
4653 // ----- Look for extract by name rule
4654 if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
4655 && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
4656 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
4658 // ----- Look if the filename is in the list
4659 for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
4660 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
4662 // ----- Look for a directory
4663 if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
4664 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
4666 // ----- Look if the directory is in the filename path
4667 if ( (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
4668 && (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])) {
4669 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
4670 $v_found = true;
4672 elseif ( (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
4673 && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
4674 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The entry is the searched directory");
4675 $v_found = true;
4678 // ----- Look for a filename
4679 elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
4680 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
4681 $v_found = true;
4686 // ----- Look for extract by ereg rule
4687 else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
4688 && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
4689 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
4691 if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
4692 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
4693 $v_found = true;
4697 // ----- Look for extract by preg rule
4698 else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
4699 && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
4700 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
4702 if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
4703 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
4704 $v_found = true;
4708 // ----- Look for extract by index rule
4709 else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
4710 && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
4711 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
4713 // ----- Look if the index is in the list
4714 for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
4715 //--(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']."]");
4717 if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
4718 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
4719 $v_found = true;
4721 if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
4722 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
4723 $j_start = $j+1;
4726 if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
4727 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
4728 break;
4732 else {
4733 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "No argument mean remove all file");
4734 $v_found = true;
4737 // ----- Look for deletion
4738 if ($v_found)
4740 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' need to be deleted");
4741 unset($v_header_list[$v_nb_extracted]);
4743 else
4745 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' will not be deleted");
4746 $v_nb_extracted++;
4750 // ----- Look if something need to be deleted
4751 if ($v_nb_extracted > 0) {
4753 // ----- Creates a temporay file
4754 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
4756 // ----- Creates a temporary zip archive
4757 $v_temp_zip = new PclZip($v_zip_temp_name);
4759 // ----- Open the temporary zip file in write mode
4760 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary write mode");
4761 if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
4762 $this->privCloseFd();
4764 // ----- Return
4765 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4766 return $v_result;
4769 // ----- Look which file need to be kept
4770 for ($i=0; $i<sizeof($v_header_list); $i++) {
4771 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Keep entry index '$i' : '".$v_header_list[$i]['filename']."'");
4773 // ----- Calculate the position of the header
4774 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset='". $v_header_list[$i]['offset']."'");
4775 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
4776 @rewind($this->zip_fd);
4777 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
4778 if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
4779 // ----- Close the zip file
4780 $this->privCloseFd();
4781 $v_temp_zip->privCloseFd();
4782 @unlink($v_zip_temp_name);
4784 // ----- Error log
4785 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
4787 // ----- Return
4788 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4789 return PclZip::errorCode();
4791 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
4793 // ----- Read the file header
4794 $v_local_header = array();
4795 if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
4796 // ----- Close the zip file
4797 $this->privCloseFd();
4798 $v_temp_zip->privCloseFd();
4799 @unlink($v_zip_temp_name);
4801 // ----- Return
4802 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4803 return $v_result;
4806 // ----- Check that local file header is same as central file header
4807 if ($this->privCheckFileHeaders($v_local_header,
4808 $v_header_list[$i]) != 1) {
4809 // TBC
4811 unset($v_local_header);
4813 // ----- Write the file header
4814 if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
4815 // ----- Close the zip file
4816 $this->privCloseFd();
4817 $v_temp_zip->privCloseFd();
4818 @unlink($v_zip_temp_name);
4820 // ----- Return
4821 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4822 return $v_result;
4824 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset for this file is '".$v_header_list[$i]['offset']."'");
4826 // ----- Read/write the data block
4827 if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
4828 // ----- Close the zip file
4829 $this->privCloseFd();
4830 $v_temp_zip->privCloseFd();
4831 @unlink($v_zip_temp_name);
4833 // ----- Return
4834 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4835 return $v_result;
4839 // ----- Store the offset of the central dir
4840 $v_offset = @ftell($v_temp_zip->zip_fd);
4841 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "New offset of central dir : $v_offset");
4843 // ----- Re-Create the Central Dir files header
4844 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the new central directory");
4845 for ($i=0; $i<sizeof($v_header_list); $i++) {
4846 // ----- Create the file header
4847 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset of file : ".$v_header_list[$i]['offset']);
4848 if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
4849 $v_temp_zip->privCloseFd();
4850 $this->privCloseFd();
4851 @unlink($v_zip_temp_name);
4853 // ----- Return
4854 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4855 return $v_result;
4858 // ----- Transform the header to a 'usable' info
4859 $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
4862 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the central directory footer");
4864 // ----- Zip file comment
4865 $v_comment = '';
4866 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
4867 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
4870 // ----- Calculate the size of the central header
4871 $v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
4873 // ----- Create the central dir footer
4874 if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
4875 // ----- Reset the file list
4876 unset($v_header_list);
4877 $v_temp_zip->privCloseFd();
4878 $this->privCloseFd();
4879 @unlink($v_zip_temp_name);
4881 // ----- Return
4882 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4883 return $v_result;
4886 // ----- Close
4887 $v_temp_zip->privCloseFd();
4888 $this->privCloseFd();
4890 // ----- Delete the zip file
4891 // TBC : I should test the result ...
4892 @unlink($this->zipname);
4894 // ----- Rename the temporary file
4895 // TBC : I should test the result ...
4896 //@rename($v_zip_temp_name, $this->zipname);
4897 PclZipUtilRename($v_zip_temp_name, $this->zipname);
4899 // ----- Destroy the temporary archive
4900 unset($v_temp_zip);
4903 // ----- Remove every files : reset the file
4904 else if ($v_central_dir['entries'] != 0) {
4905 $this->privCloseFd();
4907 if (($v_result = $this->privOpenFd('wb')) != 1) {
4908 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4909 return $v_result;
4912 if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
4913 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4914 return $v_result;
4917 $this->privCloseFd();
4920 // ----- Return
4921 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4922 return $v_result;
4924 // --------------------------------------------------------------------------------
4926 // --------------------------------------------------------------------------------
4927 // Function : privDirCheck()
4928 // Description :
4929 // Check if a directory exists, if not it creates it and all the parents directory
4930 // which may be useful.
4931 // Parameters :
4932 // $p_dir : Directory path to check.
4933 // Return Values :
4934 // 1 : OK
4935 // -1 : Unable to create directory
4936 // --------------------------------------------------------------------------------
4937 function privDirCheck($p_dir, $p_is_dir=false)
4939 $v_result = 1;
4941 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDirCheck", "entry='$p_dir', is_dir='".($p_is_dir?"true":"false")."'");
4943 // ----- Remove the final '/'
4944 if (($p_is_dir) && (substr($p_dir, -1)=='/'))
4946 $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
4948 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Looking for entry '$p_dir'");
4950 // ----- Check the directory availability
4951 if ((is_dir($p_dir)) || ($p_dir == ""))
4953 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
4954 return 1;
4957 // ----- Extract parent directory
4958 $p_parent_dir = dirname($p_dir);
4959 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
4961 // ----- Just a check
4962 if ($p_parent_dir != $p_dir)
4964 // ----- Look for parent directory
4965 if ($p_parent_dir != "")
4967 if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
4969 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4970 return $v_result;
4975 // ----- Create the directory
4976 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
4977 if (!@mkdir($p_dir, 0777))
4979 // ----- Error log
4980 PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
4982 // ----- Return
4983 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4984 return PclZip::errorCode();
4987 // ----- Return
4988 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
4989 return $v_result;
4991 // --------------------------------------------------------------------------------
4993 // --------------------------------------------------------------------------------
4994 // Function : privMerge()
4995 // Description :
4996 // If $p_archive_to_add does not exist, the function exit with a success result.
4997 // Parameters :
4998 // Return Values :
4999 // --------------------------------------------------------------------------------
5000 function privMerge(&$p_archive_to_add)
5002 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privMerge", "archive='".$p_archive_to_add->zipname."'");
5003 $v_result=1;
5005 // ----- Look if the archive_to_add exists
5006 if (!is_file($p_archive_to_add->zipname))
5008 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to add does not exist. End of merge.");
5010 // ----- Nothing to merge, so merge is a success
5011 $v_result = 1;
5013 // ----- Return
5014 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5015 return $v_result;
5018 // ----- Look if the archive exists
5019 if (!is_file($this->zipname))
5021 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, duplicate the archive_to_add.");
5023 // ----- Do a duplicate
5024 $v_result = $this->privDuplicate($p_archive_to_add->zipname);
5026 // ----- Return
5027 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5028 return $v_result;
5031 // ----- Open the zip file
5032 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
5033 if (($v_result=$this->privOpenFd('rb')) != 1)
5035 // ----- Return
5036 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5037 return $v_result;
5040 // ----- Read the central directory informations
5041 $v_central_dir = array();
5042 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
5044 $this->privCloseFd();
5045 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5046 return $v_result;
5049 // ----- Go to beginning of File
5050 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
5051 @rewind($this->zip_fd);
5052 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
5054 // ----- Open the archive_to_add file
5055 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open archive_to_add in binary read mode");
5056 if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
5058 $this->privCloseFd();
5060 // ----- Return
5061 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5062 return $v_result;
5065 // ----- Read the central directory informations
5066 $v_central_dir_to_add = array();
5067 if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
5069 $this->privCloseFd();
5070 $p_archive_to_add->privCloseFd();
5072 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5073 return $v_result;
5076 // ----- Go to beginning of File
5077 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
5078 @rewind($p_archive_to_add->zip_fd);
5079 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
5081 // ----- Creates a temporay file
5082 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
5084 // ----- Open the temporary file in write mode
5085 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
5086 if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
5088 $this->privCloseFd();
5089 $p_archive_to_add->privCloseFd();
5091 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
5093 // ----- Return
5094 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5095 return PclZip::errorCode();
5098 // ----- Copy the files from the archive to the temporary file
5099 // TBC : Here I should better append the file and go back to erase the central dir
5100 $v_size = $v_central_dir['offset'];
5101 while ($v_size != 0)
5103 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5104 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5105 $v_buffer = fread($this->zip_fd, $v_read_size);
5106 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5107 $v_size -= $v_read_size;
5110 // ----- Copy the files from the archive_to_add into the temporary file
5111 $v_size = $v_central_dir_to_add['offset'];
5112 while ($v_size != 0)
5114 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5115 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5116 $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
5117 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5118 $v_size -= $v_read_size;
5121 // ----- Store the offset of the central dir
5122 $v_offset = @ftell($v_zip_temp_fd);
5123 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
5125 // ----- Copy the block of file headers from the old archive
5126 $v_size = $v_central_dir['size'];
5127 while ($v_size != 0)
5129 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5130 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5131 $v_buffer = @fread($this->zip_fd, $v_read_size);
5132 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5133 $v_size -= $v_read_size;
5136 // ----- Copy the block of file headers from the archive_to_add
5137 $v_size = $v_central_dir_to_add['size'];
5138 while ($v_size != 0)
5140 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5141 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5142 $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
5143 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5144 $v_size -= $v_read_size;
5147 // ----- Merge the file comments
5148 $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
5150 // ----- Calculate the size of the (new) central header
5151 $v_size = @ftell($v_zip_temp_fd)-$v_offset;
5153 // ----- Swap the file descriptor
5154 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
5155 // the following methods on the temporary fil and not the real archive fd
5156 $v_swap = $this->zip_fd;
5157 $this->zip_fd = $v_zip_temp_fd;
5158 $v_zip_temp_fd = $v_swap;
5160 // ----- Create the central dir footer
5161 if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
5163 $this->privCloseFd();
5164 $p_archive_to_add->privCloseFd();
5165 @fclose($v_zip_temp_fd);
5166 $this->zip_fd = null;
5168 // ----- Reset the file list
5169 unset($v_header_list);
5171 // ----- Return
5172 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5173 return $v_result;
5176 // ----- Swap back the file descriptor
5177 $v_swap = $this->zip_fd;
5178 $this->zip_fd = $v_zip_temp_fd;
5179 $v_zip_temp_fd = $v_swap;
5181 // ----- Close
5182 $this->privCloseFd();
5183 $p_archive_to_add->privCloseFd();
5185 // ----- Close the temporary file
5186 @fclose($v_zip_temp_fd);
5188 // ----- Delete the zip file
5189 // TBC : I should test the result ...
5190 @unlink($this->zipname);
5192 // ----- Rename the temporary file
5193 // TBC : I should test the result ...
5194 //@rename($v_zip_temp_name, $this->zipname);
5195 PclZipUtilRename($v_zip_temp_name, $this->zipname);
5197 // ----- Return
5198 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5199 return $v_result;
5201 // --------------------------------------------------------------------------------
5203 // --------------------------------------------------------------------------------
5204 // Function : privDuplicate()
5205 // Description :
5206 // Parameters :
5207 // Return Values :
5208 // --------------------------------------------------------------------------------
5209 function privDuplicate($p_archive_filename)
5211 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDuplicate", "archive_filename='$p_archive_filename'");
5212 $v_result=1;
5214 // ----- Look if the $p_archive_filename exists
5215 if (!is_file($p_archive_filename))
5217 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to duplicate does not exist. End of duplicate.");
5219 // ----- Nothing to duplicate, so duplicate is a success.
5220 $v_result = 1;
5222 // ----- Return
5223 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5224 return $v_result;
5227 // ----- Open the zip file
5228 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
5229 if (($v_result=$this->privOpenFd('wb')) != 1)
5231 // ----- Return
5232 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5233 return $v_result;
5236 // ----- Open the temporary file in write mode
5237 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
5238 if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
5240 $this->privCloseFd();
5242 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
5244 // ----- Return
5245 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5246 return PclZip::errorCode();
5249 // ----- Copy the files from the archive to the temporary file
5250 // TBC : Here I should better append the file and go back to erase the central dir
5251 $v_size = filesize($p_archive_filename);
5252 while ($v_size != 0)
5254 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5255 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read $v_read_size bytes");
5256 $v_buffer = fread($v_zip_temp_fd, $v_read_size);
5257 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
5258 $v_size -= $v_read_size;
5261 // ----- Close
5262 $this->privCloseFd();
5264 // ----- Close the temporary file
5265 @fclose($v_zip_temp_fd);
5267 // ----- Return
5268 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5269 return $v_result;
5271 // --------------------------------------------------------------------------------
5273 // --------------------------------------------------------------------------------
5274 // Function : privErrorLog()
5275 // Description :
5276 // Parameters :
5277 // --------------------------------------------------------------------------------
5278 function privErrorLog($p_error_code=0, $p_error_string='')
5280 if (PCLZIP_ERROR_EXTERNAL == 1) {
5281 PclError($p_error_code, $p_error_string);
5283 else {
5284 $this->error_code = $p_error_code;
5285 $this->error_string = $p_error_string;
5288 // --------------------------------------------------------------------------------
5290 // --------------------------------------------------------------------------------
5291 // Function : privErrorReset()
5292 // Description :
5293 // Parameters :
5294 // --------------------------------------------------------------------------------
5295 function privErrorReset()
5297 if (PCLZIP_ERROR_EXTERNAL == 1) {
5298 PclErrorReset();
5300 else {
5301 $this->error_code = 0;
5302 $this->error_string = '';
5305 // --------------------------------------------------------------------------------
5307 // --------------------------------------------------------------------------------
5308 // Function : privDecrypt()
5309 // Description :
5310 // Parameters :
5311 // Return Values :
5312 // --------------------------------------------------------------------------------
5313 function privDecrypt($p_encryption_header, &$p_buffer, $p_size, $p_crc)
5315 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privDecrypt', "size=".$p_size."");
5316 $v_result=1;
5318 // ----- To Be Modified ;-)
5319 $v_pwd = "test";
5321 $p_buffer = PclZipUtilZipDecrypt($p_buffer, $p_size, $p_encryption_header,
5322 $p_crc, $v_pwd);
5324 // ----- Return
5325 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5326 return $v_result;
5328 // --------------------------------------------------------------------------------
5330 // --------------------------------------------------------------------------------
5331 // Function : privDisableMagicQuotes()
5332 // Description :
5333 // Parameters :
5334 // Return Values :
5335 // --------------------------------------------------------------------------------
5336 function privDisableMagicQuotes()
5338 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privDisableMagicQuotes', "");
5339 $v_result=1;
5341 // ----- Look if function exists
5342 if ( (!function_exists("get_magic_quotes_runtime"))
5343 || (!function_exists("set_magic_quotes_runtime"))) {
5344 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Functions *et_magic_quotes_runtime are not supported");
5345 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5346 return $v_result;
5349 // ----- Look if already done
5350 if ($this->magic_quotes_status != -1) {
5351 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "magic_quote already disabled");
5352 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5353 return $v_result;
5356 // ----- Get and memorize the magic_quote value
5357 $this->magic_quotes_status = @get_magic_quotes_runtime();
5358 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Current magic_quotes_runtime status is '".($this->magic_quotes_status==0?'disable':'enable')."'");
5360 // ----- Disable magic_quotes
5361 if ($this->magic_quotes_status == 1) {
5362 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Disable magic_quotes");
5363 @set_magic_quotes_runtime(0);
5366 // ----- Return
5367 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5368 return $v_result;
5370 // --------------------------------------------------------------------------------
5372 // --------------------------------------------------------------------------------
5373 // Function : privSwapBackMagicQuotes()
5374 // Description :
5375 // Parameters :
5376 // Return Values :
5377 // --------------------------------------------------------------------------------
5378 function privSwapBackMagicQuotes()
5380 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privSwapBackMagicQuotes', "");
5381 $v_result=1;
5383 // ----- Look if function exists
5384 if ( (!function_exists("get_magic_quotes_runtime"))
5385 || (!function_exists("set_magic_quotes_runtime"))) {
5386 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Functions *et_magic_quotes_runtime are not supported");
5387 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5388 return $v_result;
5391 // ----- Look if something to do
5392 if ($this->magic_quotes_status != -1) {
5393 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "magic_quote not modified");
5394 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5395 return $v_result;
5398 // ----- Swap back magic_quotes
5399 if ($this->magic_quotes_status == 1) {
5400 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Enable back magic_quotes");
5401 @set_magic_quotes_runtime($this->magic_quotes_status);
5404 // ----- Return
5405 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5406 return $v_result;
5408 // --------------------------------------------------------------------------------
5411 // End of class
5412 // --------------------------------------------------------------------------------
5414 // --------------------------------------------------------------------------------
5415 // Function : PclZipUtilPathReduction()
5416 // Description :
5417 // Parameters :
5418 // Return Values :
5419 // --------------------------------------------------------------------------------
5420 function PclZipUtilPathReduction($p_dir)
5422 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathReduction", "dir='$p_dir'");
5423 $v_result = "";
5425 // ----- Look for not empty path
5426 if ($p_dir != "") {
5427 // ----- Explode path by directory names
5428 $v_list = explode("/", $p_dir);
5430 // ----- Study directories from last to first
5431 $v_skip = 0;
5432 for ($i=sizeof($v_list)-1; $i>=0; $i--) {
5433 // ----- Look for current path
5434 if ($v_list[$i] == ".") {
5435 // ----- Ignore this directory
5436 // Should be the first $i=0, but no check is done
5438 else if ($v_list[$i] == "..") {
5439 $v_skip++;
5441 else if ($v_list[$i] == "") {
5442 // ----- First '/' i.e. root slash
5443 if ($i == 0) {
5444 $v_result = "/".$v_result;
5445 if ($v_skip > 0) {
5446 // ----- It is an invalid path, so the path is not modified
5447 // TBC
5448 $v_result = $p_dir;
5449 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid path is unchanged");
5450 $v_skip = 0;
5453 // ----- Last '/' i.e. indicates a directory
5454 else if ($i == (sizeof($v_list)-1)) {
5455 $v_result = $v_list[$i];
5457 // ----- Double '/' inside the path
5458 else {
5459 // ----- Ignore only the double '//' in path,
5460 // but not the first and last '/'
5463 else {
5464 // ----- Look for item to skip
5465 if ($v_skip > 0) {
5466 $v_skip--;
5468 else {
5469 $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
5474 // ----- Look for skip
5475 if ($v_skip > 0) {
5476 while ($v_skip > 0) {
5477 $v_result = '../'.$v_result;
5478 $v_skip--;
5483 // ----- Return
5484 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5485 return $v_result;
5487 // --------------------------------------------------------------------------------
5489 // --------------------------------------------------------------------------------
5490 // Function : PclZipUtilPathInclusion()
5491 // Description :
5492 // This function indicates if the path $p_path is under the $p_dir tree. Or,
5493 // said in an other way, if the file or sub-dir $p_path is inside the dir
5494 // $p_dir.
5495 // The function indicates also if the path is exactly the same as the dir.
5496 // This function supports path with duplicated '/' like '//', but does not
5497 // support '.' or '..' statements.
5498 // Parameters :
5499 // Return Values :
5500 // 0 if $p_path is not inside directory $p_dir
5501 // 1 if $p_path is inside directory $p_dir
5502 // 2 if $p_path is exactly the same as $p_dir
5503 // --------------------------------------------------------------------------------
5504 function PclZipUtilPathInclusion($p_dir, $p_path)
5506 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathInclusion", "dir='$p_dir', path='$p_path'");
5507 $v_result = 1;
5509 // ----- Look for path beginning by ./
5510 if ( ($p_dir == '.')
5511 || ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
5512 $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
5513 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Replacing ./ by full path in p_dir '".$p_dir."'");
5515 if ( ($p_path == '.')
5516 || ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
5517 $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
5518 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Replacing ./ by full path in p_path '".$p_path."'");
5521 // ----- Explode dir and path by directory separator
5522 $v_list_dir = explode("/", $p_dir);
5523 $v_list_dir_size = sizeof($v_list_dir);
5524 $v_list_path = explode("/", $p_path);
5525 $v_list_path_size = sizeof($v_list_path);
5527 // ----- Study directories paths
5528 $i = 0;
5529 $j = 0;
5530 while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
5531 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Working on dir($i)='".$v_list_dir[$i]."' and path($j)='".$v_list_path[$j]."'");
5533 // ----- Look for empty dir (path reduction)
5534 if ($v_list_dir[$i] == '') {
5535 $i++;
5536 continue;
5538 if ($v_list_path[$j] == '') {
5539 $j++;
5540 continue;
5543 // ----- Compare the items
5544 if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) {
5545 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Items ($i,$j) are different");
5546 $v_result = 0;
5549 // ----- Next items
5550 $i++;
5551 $j++;
5554 // ----- Look if everything seems to be the same
5555 if ($v_result) {
5556 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Look for tie break");
5557 // ----- Skip all the empty items
5558 while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
5559 while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
5560 //--(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]:'')."'");
5562 if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
5563 // ----- There are exactly the same
5564 $v_result = 2;
5566 else if ($i < $v_list_dir_size) {
5567 // ----- The path is shorter than the dir
5568 $v_result = 0;
5572 // ----- Return
5573 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5574 return $v_result;
5576 // --------------------------------------------------------------------------------
5578 // --------------------------------------------------------------------------------
5579 // Function : PclZipUtilCopyBlock()
5580 // Description :
5581 // Parameters :
5582 // $p_mode : read/write compression mode
5583 // 0 : src & dest normal
5584 // 1 : src gzip, dest normal
5585 // 2 : src normal, dest gzip
5586 // 3 : src & dest gzip
5587 // Return Values :
5588 // --------------------------------------------------------------------------------
5589 function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
5591 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilCopyBlock", "size=$p_size, mode=$p_mode");
5592 $v_result = 1;
5594 if ($p_mode==0)
5596 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset before read :".(@ftell($p_src)));
5597 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset before write :".(@ftell($p_dest)));
5598 while ($p_size != 0)
5600 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5601 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5602 $v_buffer = @fread($p_src, $v_read_size);
5603 @fwrite($p_dest, $v_buffer, $v_read_size);
5604 $p_size -= $v_read_size;
5606 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset after read :".(@ftell($p_src)));
5607 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset after write :".(@ftell($p_dest)));
5609 else if ($p_mode==1)
5611 while ($p_size != 0)
5613 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5614 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5615 $v_buffer = @gzread($p_src, $v_read_size);
5616 @fwrite($p_dest, $v_buffer, $v_read_size);
5617 $p_size -= $v_read_size;
5620 else if ($p_mode==2)
5622 while ($p_size != 0)
5624 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5625 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5626 $v_buffer = @fread($p_src, $v_read_size);
5627 @gzwrite($p_dest, $v_buffer, $v_read_size);
5628 $p_size -= $v_read_size;
5631 else if ($p_mode==3)
5633 while ($p_size != 0)
5635 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5636 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5637 $v_buffer = @gzread($p_src, $v_read_size);
5638 @gzwrite($p_dest, $v_buffer, $v_read_size);
5639 $p_size -= $v_read_size;
5643 // ----- Return
5644 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5645 return $v_result;
5647 // --------------------------------------------------------------------------------
5649 // --------------------------------------------------------------------------------
5650 // Function : PclZipUtilRename()
5651 // Description :
5652 // This function tries to do a simple rename() function. If it fails, it
5653 // tries to copy the $p_src file in a new $p_dest file and then unlink the
5654 // first one.
5655 // Parameters :
5656 // $p_src : Old filename
5657 // $p_dest : New filename
5658 // Return Values :
5659 // 1 on success, 0 on failure.
5660 // --------------------------------------------------------------------------------
5661 function PclZipUtilRename($p_src, $p_dest)
5663 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilRename", "source=$p_src, destination=$p_dest");
5664 $v_result = 1;
5666 // ----- Try to rename the files
5667 if (!@rename($p_src, $p_dest)) {
5668 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to rename file, try copy+unlink");
5670 // ----- Try to copy & unlink the src
5671 if (!@copy($p_src, $p_dest)) {
5672 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to copy file");
5673 $v_result = 0;
5675 else if (!@unlink($p_src)) {
5676 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to unlink old filename");
5677 $v_result = 0;
5681 // ----- Return
5682 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5683 return $v_result;
5685 // --------------------------------------------------------------------------------
5687 // --------------------------------------------------------------------------------
5688 // Function : PclZipUtilOptionText()
5689 // Description :
5690 // Translate option value in text. Mainly for debug purpose.
5691 // Parameters :
5692 // $p_option : the option value.
5693 // Return Values :
5694 // The option text value.
5695 // --------------------------------------------------------------------------------
5696 function PclZipUtilOptionText($p_option)
5698 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilOptionText", "option='".$p_option."'");
5700 $v_list = get_defined_constants();
5701 for (reset($v_list); $v_key = key($v_list); next($v_list)) {
5702 $v_prefix = substr($v_key, 0, 10);
5703 if (( ($v_prefix == 'PCLZIP_OPT')
5704 || ($v_prefix == 'PCLZIP_CB_')
5705 || ($v_prefix == 'PCLZIP_ATT'))
5706 && ($v_list[$v_key] == $p_option)) {
5707 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_key);
5708 return $v_key;
5712 $v_result = 'Unknown';
5714 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5715 return $v_result;
5717 // --------------------------------------------------------------------------------
5719 // --------------------------------------------------------------------------------
5720 // Function : PclZipUtilTranslateWinPath()
5721 // Description :
5722 // Translate windows path by replacing '\' by '/' and optionally removing
5723 // drive letter.
5724 // Parameters :
5725 // $p_path : path to translate.
5726 // $p_remove_disk_letter : true | false
5727 // Return Values :
5728 // The path translated.
5729 // --------------------------------------------------------------------------------
5730 function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
5732 if (stristr(php_uname(), 'windows')) {
5733 // ----- Look for potential disk letter
5734 if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
5735 $p_path = substr($p_path, $v_position+1);
5737 // ----- Change potential windows directory separator
5738 if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
5739 $p_path = strtr($p_path, '\\', '/');
5742 return $p_path;
5744 // --------------------------------------------------------------------------------