2 // --------------------------------------------------------------------------------
3 // PhpConcept Library - Zip Module 2.8
4 // --------------------------------------------------------------------------------
5 // License GNU/LGPL - Vincent Blavet - March 2006
6 // http://www.phpconcept.net
7 // --------------------------------------------------------------------------------
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.
15 // See readme.txt and http://www.phpconcept.net
18 // This library and the associated files are non commercial, non professional
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: pclzip.lib.php,v 1.55 2009/04/22 07:38:36 vblavet Exp $
26 // --------------------------------------------------------------------------------
29 if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
30 define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
33 // ----- File list separator
34 // In version 1.x of PclZip, the separator for file list is a space
35 // (which is not a very smart choice, specifically for windows paths !).
36 // A better separator should be a comma (,). This constant gives you the
37 // abilty to change that.
38 // However notice that changing this value, may have impact on existing
39 // scripts, using space separated filenames.
40 // Recommanded values for compatibility with older versions :
41 //define( 'PCLZIP_SEPARATOR', ' ' );
42 // Recommanded values for smart separation of filenames.
43 if (!defined('PCLZIP_SEPARATOR')) {
44 define( 'PCLZIP_SEPARATOR', ',' );
47 // ----- Error configuration
48 // 0 : PclZip Class integrated error handling
49 // 1 : PclError external library error handling. By enabling this
50 // you must ensure that you have included PclError library.
51 // [2,...] : reserved for futur use
52 if (!defined('PCLZIP_ERROR_EXTERNAL')) {
53 define( 'PCLZIP_ERROR_EXTERNAL', 0 );
56 // ----- Optional static temporary directory
57 // By default temporary files are generated in the script current
60 // - MUST BE terminated by a '/'.
61 // - MUST be a valid, already created directory
63 // define( 'PCLZIP_TEMPORARY_DIR', '/temp/' );
64 // define( 'PCLZIP_TEMPORARY_DIR', 'C:/Temp/' );
65 if (!defined('PCLZIP_TEMPORARY_DIR')) {
66 define( 'PCLZIP_TEMPORARY_DIR', '' );
69 // ----- Optional threshold ratio for use of temporary files
70 // Pclzip sense the size of the file to add/extract and decide to
71 // use or not temporary file. The algorythm is looking for
72 // memory_limit of PHP and apply a ratio.
73 // threshold = memory_limit * ratio.
74 // Recommended values are under 0.5. Default 0.47.
76 // define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.5 );
77 if (!defined('PCLZIP_TEMPORARY_FILE_RATIO')) {
78 define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.47 );
81 // --------------------------------------------------------------------------------
82 // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
83 // --------------------------------------------------------------------------------
85 // ----- Global variables
86 $g_pclzip_version = "2.8";
89 // -1 : Unable to open file in binary write mode
90 // -2 : Unable to open file in binary read mode
91 // -3 : Invalid parameters
92 // -4 : File does not exist
93 // -5 : Filename is too long (max. 255)
94 // -6 : Not a valid zip file
95 // -7 : Invalid extracted file size
96 // -8 : Unable to create directory
97 // -9 : Invalid archive extension
98 // -10 : Invalid archive format
99 // -11 : Unable to delete file (unlink)
100 // -12 : Unable to rename file (rename)
101 // -13 : Invalid header checksum
102 // -14 : Invalid archive size
103 define( 'PCLZIP_ERR_USER_ABORTED', 2 );
104 define( 'PCLZIP_ERR_NO_ERROR', 0 );
105 define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
106 define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
107 define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
108 define( 'PCLZIP_ERR_MISSING_FILE', -4 );
109 define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
110 define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
111 define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
112 define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
113 define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
114 define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
115 define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
116 define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
117 define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
118 define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
119 define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
120 define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
121 define( 'PCLZIP_ERR_ALREADY_A_DIRECTORY', -17 );
122 define( 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18 );
123 define( 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19 );
124 define( 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20 );
125 define( 'PCLZIP_ERR_DIRECTORY_RESTRICTION', -21 );
127 // ----- Options values
128 define( 'PCLZIP_OPT_PATH', 77001 );
129 define( 'PCLZIP_OPT_ADD_PATH', 77002 );
130 define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
131 define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
132 define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
133 define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
134 define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
135 define( 'PCLZIP_OPT_BY_NAME', 77008 );
136 define( 'PCLZIP_OPT_BY_INDEX', 77009 );
137 define( 'PCLZIP_OPT_BY_EREG', 77010 );
138 define( 'PCLZIP_OPT_BY_PREG', 77011 );
139 define( 'PCLZIP_OPT_COMMENT', 77012 );
140 define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
141 define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
142 define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
143 define( 'PCLZIP_OPT_REPLACE_NEWER', 77016 );
144 define( 'PCLZIP_OPT_STOP_ON_ERROR', 77017 );
145 // Having big trouble with crypt. Need to multiply 2 long int
146 // which is not correctly supported by PHP ...
147 //define( 'PCLZIP_OPT_CRYPT', 77018 );
148 define( 'PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019 );
149 define( 'PCLZIP_OPT_TEMP_FILE_THRESHOLD', 77020 );
150 define( 'PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020 ); // alias
151 define( 'PCLZIP_OPT_TEMP_FILE_ON', 77021 );
152 define( 'PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021 ); // alias
153 define( 'PCLZIP_OPT_TEMP_FILE_OFF', 77022 );
154 define( 'PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022 ); // alias
156 // ----- File description attributes
157 define( 'PCLZIP_ATT_FILE_NAME', 79001 );
158 define( 'PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002 );
159 define( 'PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003 );
160 define( 'PCLZIP_ATT_FILE_MTIME', 79004 );
161 define( 'PCLZIP_ATT_FILE_CONTENT', 79005 );
162 define( 'PCLZIP_ATT_FILE_COMMENT', 79006 );
164 // ----- Call backs values
165 define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
166 define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
167 define( 'PCLZIP_CB_PRE_ADD', 78003 );
168 define( 'PCLZIP_CB_POST_ADD', 78004 );
170 define( 'PCLZIP_CB_PRE_LIST', 78005 );
171 define( 'PCLZIP_CB_POST_LIST', 78006 );
172 define( 'PCLZIP_CB_PRE_DELETE', 78007 );
173 define( 'PCLZIP_CB_POST_DELETE', 78008 );
176 // --------------------------------------------------------------------------------
179 // PclZip is the class that represent a Zip archive.
180 // The public methods allow the manipulation of the archive.
182 // Attributes must not be accessed directly.
184 // PclZip() : Object creator
185 // create() : Creates the Zip archive
186 // listContent() : List the content of the Zip archive
187 // extract() : Extract the content of the archive
188 // properties() : List the properties of the archive
189 // --------------------------------------------------------------------------------
192 // ----- Filename of the zip file
195 // ----- File descriptor of the zip file
198 // ----- Internal error handling
200 var $error_string = '';
202 // ----- Current status of the magic_quotes_runtime
203 // This value store the php configuration for magic_quotes
204 // The class can then disable the magic_quotes and reset it after
205 var $magic_quotes_status;
207 // --------------------------------------------------------------------------------
208 // Function : PclZip()
210 // Creates a PclZip object and set the name of the associated Zip archive
212 // Note that no real action is taken, if the archive does not exist it is not
213 // created. Use create() for that.
214 // --------------------------------------------------------------------------------
215 function PclZip($p_zipname)
217 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::PclZip', "zipname=$p_zipname");
219 // ----- Tests the zlib
220 if (!function_exists('gzopen'))
222 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 1, "zlib extension seems to be missing");
223 die('Abort '.basename(__FILE__).' : Missing zlib extensions');
226 // ----- Set the attributes
227 $this->zipname = $p_zipname;
229 $this->magic_quotes_status = -1;
232 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 1);
235 // --------------------------------------------------------------------------------
237 // --------------------------------------------------------------------------------
239 // create($p_filelist, $p_add_dir="", $p_remove_dir="")
240 // create($p_filelist, $p_option, $p_option_value, ...)
242 // This method supports two different synopsis. The first one is historical.
243 // This method creates a Zip Archive. The Zip file is created in the
244 // filesystem. The files and directories indicated in $p_filelist
245 // are added in the archive. See the parameters description for the
246 // supported format of $p_filelist.
247 // When a directory is in the list, the directory and its content is added
249 // In this synopsis, the function takes an optional variable list of
250 // options. See bellow the supported options.
252 // $p_filelist : An array containing file or directory names, or
253 // a string containing one filename or one directory name, or
254 // a string containing a list of filenames and/or directory
255 // names separated by spaces.
256 // $p_add_dir : A path to add before the real path of the archived file,
257 // in order to have it memorized in the archive.
258 // $p_remove_dir : A path to remove from the real path of the file to archive,
259 // in order to have a shorter path memorized in the archive.
260 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
261 // is removed first, before $p_add_dir is added.
263 // PCLZIP_OPT_ADD_PATH :
264 // PCLZIP_OPT_REMOVE_PATH :
265 // PCLZIP_OPT_REMOVE_ALL_PATH :
266 // PCLZIP_OPT_COMMENT :
267 // PCLZIP_CB_PRE_ADD :
268 // PCLZIP_CB_POST_ADD :
271 // The list of the added files, with a status of the add action.
272 // (see PclZip::listContent() for list entry format)
273 // --------------------------------------------------------------------------------
274 function create($p_filelist)
276 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::create', "filelist='$p_filelist', ...");
279 // ----- Reset the error handler
280 $this->privErrorReset();
282 // ----- Set default values
283 $v_options = array();
284 $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
286 // ----- Look for variable options arguments
287 $v_size = func_num_args();
288 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
290 // ----- Look for arguments
292 // ----- Get the arguments
293 $v_arg_list = func_get_args();
295 // ----- Remove from the options list the first argument
296 array_shift($v_arg_list);
299 // ----- Look for first arg
300 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
301 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
303 // ----- Parse the options
304 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
305 array (PCLZIP_OPT_REMOVE_PATH => 'optional',
306 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
307 PCLZIP_OPT_ADD_PATH => 'optional',
308 PCLZIP_CB_PRE_ADD => 'optional',
309 PCLZIP_CB_POST_ADD => 'optional',
310 PCLZIP_OPT_NO_COMPRESSION => 'optional',
311 PCLZIP_OPT_COMMENT => 'optional',
312 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
313 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
314 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
315 //, PCLZIP_OPT_CRYPT => 'optional'
317 if ($v_result != 1) {
318 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
323 // ----- Look for 2 args
324 // Here we need to support the first historic synopsis of the
327 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
329 // ----- Get the first argument
330 $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
332 // ----- Look for the optional second argument
334 $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
336 else if ($v_size > 2) {
337 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
338 "Invalid number / type of arguments");
339 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
345 // ----- Look for default option values
346 $this->privOptionDefaultThreshold($v_options);
349 $v_string_list = array();
350 $v_att_list = array();
351 $v_filedescr_list = array();
352 $p_result_list = array();
354 // ----- Look if the $p_filelist is really an array
355 if (is_array($p_filelist)) {
357 // ----- Look if the first element is also an array
358 // This will mean that this is a file description entry
359 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
360 $v_att_list = $p_filelist;
363 // ----- The list is a list of string names
365 $v_string_list = $p_filelist;
369 // ----- Look if the $p_filelist is a string
370 else if (is_string($p_filelist)) {
371 // ----- Create a list from the string
372 $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
375 // ----- Invalid variable type for $p_filelist
377 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
378 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
382 // ----- Reformat the string list
383 if (sizeof($v_string_list) != 0) {
384 foreach ($v_string_list as $v_string) {
385 if ($v_string != '') {
386 $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
389 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Ignore an empty filename");
394 // ----- For each file in the list check the attributes
395 $v_supported_attributes
396 = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
397 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
398 ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
399 ,PCLZIP_ATT_FILE_MTIME => 'optional'
400 ,PCLZIP_ATT_FILE_CONTENT => 'optional'
401 ,PCLZIP_ATT_FILE_COMMENT => 'optional'
403 foreach ($v_att_list as $v_entry) {
404 $v_result = $this->privFileDescrParseAtt($v_entry,
407 $v_supported_attributes);
408 if ($v_result != 1) {
409 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
414 // ----- Expand the filelist (expand directories)
415 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
416 if ($v_result != 1) {
417 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
421 // ----- Call the create fct
422 $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
423 if ($v_result != 1) {
424 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
429 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
430 return $p_result_list;
432 // --------------------------------------------------------------------------------
434 // --------------------------------------------------------------------------------
436 // add($p_filelist, $p_add_dir="", $p_remove_dir="")
437 // add($p_filelist, $p_option, $p_option_value, ...)
439 // This method supports two synopsis. The first one is historical.
440 // This methods add the list of files in an existing archive.
441 // If a file with the same name already exists, it is added at the end of the
442 // archive, the first one is still present.
443 // If the archive does not exist, it is created.
445 // $p_filelist : An array containing file or directory names, or
446 // a string containing one filename or one directory name, or
447 // a string containing a list of filenames and/or directory
448 // names separated by spaces.
449 // $p_add_dir : A path to add before the real path of the archived file,
450 // in order to have it memorized in the archive.
451 // $p_remove_dir : A path to remove from the real path of the file to archive,
452 // in order to have a shorter path memorized in the archive.
453 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
454 // is removed first, before $p_add_dir is added.
456 // PCLZIP_OPT_ADD_PATH :
457 // PCLZIP_OPT_REMOVE_PATH :
458 // PCLZIP_OPT_REMOVE_ALL_PATH :
459 // PCLZIP_OPT_COMMENT :
460 // PCLZIP_OPT_ADD_COMMENT :
461 // PCLZIP_OPT_PREPEND_COMMENT :
462 // PCLZIP_CB_PRE_ADD :
463 // PCLZIP_CB_POST_ADD :
466 // The list of the added files, with a status of the add action.
467 // (see PclZip::listContent() for list entry format)
468 // --------------------------------------------------------------------------------
469 function add($p_filelist)
471 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::add', "filelist='$p_filelist', ...");
474 // ----- Reset the error handler
475 $this->privErrorReset();
477 // ----- Set default values
478 $v_options = array();
479 $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
481 // ----- Look for variable options arguments
482 $v_size = func_num_args();
483 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
485 // ----- Look for arguments
487 // ----- Get the arguments
488 $v_arg_list = func_get_args();
490 // ----- Remove form the options list the first argument
491 array_shift($v_arg_list);
494 // ----- Look for first arg
495 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
496 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
498 // ----- Parse the options
499 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
500 array (PCLZIP_OPT_REMOVE_PATH => 'optional',
501 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
502 PCLZIP_OPT_ADD_PATH => 'optional',
503 PCLZIP_CB_PRE_ADD => 'optional',
504 PCLZIP_CB_POST_ADD => 'optional',
505 PCLZIP_OPT_NO_COMPRESSION => 'optional',
506 PCLZIP_OPT_COMMENT => 'optional',
507 PCLZIP_OPT_ADD_COMMENT => 'optional',
508 PCLZIP_OPT_PREPEND_COMMENT => 'optional',
509 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
510 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
511 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
512 //, PCLZIP_OPT_CRYPT => 'optional'
514 if ($v_result != 1) {
515 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
520 // ----- Look for 2 args
521 // Here we need to support the first historic synopsis of the
524 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
526 // ----- Get the first argument
527 $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
529 // ----- Look for the optional second argument
531 $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
533 else if ($v_size > 2) {
535 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
538 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
544 // ----- Look for default option values
545 $this->privOptionDefaultThreshold($v_options);
548 $v_string_list = array();
549 $v_att_list = array();
550 $v_filedescr_list = array();
551 $p_result_list = array();
553 // ----- Look if the $p_filelist is really an array
554 if (is_array($p_filelist)) {
556 // ----- Look if the first element is also an array
557 // This will mean that this is a file description entry
558 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
559 $v_att_list = $p_filelist;
562 // ----- The list is a list of string names
564 $v_string_list = $p_filelist;
568 // ----- Look if the $p_filelist is a string
569 else if (is_string($p_filelist)) {
570 // ----- Create a list from the string
571 $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
574 // ----- Invalid variable type for $p_filelist
576 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
577 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
581 // ----- Reformat the string list
582 if (sizeof($v_string_list) != 0) {
583 foreach ($v_string_list as $v_string) {
584 $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
588 // ----- For each file in the list check the attributes
589 $v_supported_attributes
590 = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
591 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
592 ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
593 ,PCLZIP_ATT_FILE_MTIME => 'optional'
594 ,PCLZIP_ATT_FILE_CONTENT => 'optional'
595 ,PCLZIP_ATT_FILE_COMMENT => 'optional'
597 foreach ($v_att_list as $v_entry) {
598 $v_result = $this->privFileDescrParseAtt($v_entry,
601 $v_supported_attributes);
602 if ($v_result != 1) {
603 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
608 // ----- Expand the filelist (expand directories)
609 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
610 if ($v_result != 1) {
611 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
615 // ----- Call the create fct
616 $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
617 if ($v_result != 1) {
618 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
623 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
624 return $p_result_list;
626 // --------------------------------------------------------------------------------
628 // --------------------------------------------------------------------------------
629 // Function : listContent()
631 // This public method, gives the list of the files and directories, with their
633 // The properties of each entries in the list are (used also in other functions) :
634 // filename : Name of the file. For a create or add action it is the filename
635 // given by the user. For an extract function it is the filename
636 // of the extracted file.
637 // stored_filename : Name of the file / directory stored in the archive.
638 // size : Size of the stored file.
639 // compressed_size : Size of the file's data compressed in the archive
640 // (without the headers overhead)
641 // mtime : Last known modification date of the file (UNIX timestamp)
642 // comment : Comment associated with the file
643 // folder : true | false
644 // index : index of the file in the archive
645 // status : status of the action (depending of the action) :
648 // filtered : the file / dir is not extracted (filtered by user)
649 // already_a_directory : the file can not be extracted because a
650 // directory with the same name already exists
651 // write_protected : the file can not be extracted because a file
652 // with the same name already exists and is
654 // newer_exist : the file was not extracted because a newer file exists
655 // path_creation_fail : the file is not extracted because the folder
656 // does not exist and can not be created
657 // write_error : the file was not extracted because there was a
658 // error while writing the file
659 // read_error : the file was not extracted because there was a error
660 // while reading the file
661 // invalid_header : the file was not extracted because of an archive
662 // format error (bad file header)
663 // Note that each time a method can continue operating when there
664 // is an action error on a file, the error is only logged in the file status.
666 // 0 on an unrecoverable failure,
667 // The list of the files in the archive.
668 // --------------------------------------------------------------------------------
669 function listContent()
671 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::listContent', "");
674 // ----- Reset the error handler
675 $this->privErrorReset();
677 // ----- Check archive
678 if (!$this->privCheckFormat()) {
679 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
683 // ----- Call the extracting fct
685 if (($v_result = $this->privList($p_list)) != 1)
688 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
693 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
696 // --------------------------------------------------------------------------------
698 // --------------------------------------------------------------------------------
700 // extract($p_path="./", $p_remove_path="")
701 // extract([$p_option, $p_option_value, ...])
703 // This method supports two synopsis. The first one is historical.
704 // This method extract all the files / directories from the archive to the
705 // folder indicated in $p_path.
706 // If you want to ignore the 'root' part of path of the memorized files
707 // you can indicate this in the optional $p_remove_path parameter.
708 // By default, if a newer file with the same name already exists, the
709 // file is not extracted.
711 // If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
712 // are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
713 // at the end of the path value of PCLZIP_OPT_PATH.
715 // $p_path : Path where the files and directories are to be extracted
716 // $p_remove_path : First part ('root' part) of the memorized path
717 // (if any similar) to remove while extracting.
720 // PCLZIP_OPT_ADD_PATH :
721 // PCLZIP_OPT_REMOVE_PATH :
722 // PCLZIP_OPT_REMOVE_ALL_PATH :
723 // PCLZIP_CB_PRE_EXTRACT :
724 // PCLZIP_CB_POST_EXTRACT :
726 // 0 or a negative value on failure,
727 // The list of the extracted files, with a status of the action.
728 // (see PclZip::listContent() for list entry format)
729 // --------------------------------------------------------------------------------
732 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extract", "");
735 // ----- Reset the error handler
736 $this->privErrorReset();
738 // ----- Check archive
739 if (!$this->privCheckFormat()) {
740 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
744 // ----- Set default values
745 $v_options = array();
749 $v_remove_all_path = false;
751 // ----- Look for variable options arguments
752 $v_size = func_num_args();
753 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
755 // ----- Default values for option
756 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
758 // ----- Look for arguments
760 // ----- Get the arguments
761 $v_arg_list = func_get_args();
763 // ----- Look for first arg
764 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
765 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
767 // ----- Parse the options
768 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
769 array (PCLZIP_OPT_PATH => 'optional',
770 PCLZIP_OPT_REMOVE_PATH => 'optional',
771 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
772 PCLZIP_OPT_ADD_PATH => 'optional',
773 PCLZIP_CB_PRE_EXTRACT => 'optional',
774 PCLZIP_CB_POST_EXTRACT => 'optional',
775 PCLZIP_OPT_SET_CHMOD => 'optional',
776 PCLZIP_OPT_BY_NAME => 'optional',
777 PCLZIP_OPT_BY_EREG => 'optional',
778 PCLZIP_OPT_BY_PREG => 'optional',
779 PCLZIP_OPT_BY_INDEX => 'optional',
780 PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
781 PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
782 PCLZIP_OPT_REPLACE_NEWER => 'optional'
783 ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
784 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
785 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
786 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
787 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
789 if ($v_result != 1) {
790 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
794 // ----- Set the arguments
795 if (isset($v_options[PCLZIP_OPT_PATH])) {
796 $v_path = $v_options[PCLZIP_OPT_PATH];
798 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
799 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
801 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
802 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
804 if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
805 // ----- Check for '/' in last path char
806 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
809 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
813 // ----- Look for 2 args
814 // Here we need to support the first historic synopsis of the
817 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
819 // ----- Get the first argument
820 $v_path = $v_arg_list[0];
822 // ----- Look for the optional second argument
824 $v_remove_path = $v_arg_list[1];
826 else if ($v_size > 2) {
828 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
831 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
837 // ----- Look for default option values
838 $this->privOptionDefaultThreshold($v_options);
841 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
843 // ----- Call the extracting fct
845 $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
846 $v_remove_all_path, $v_options);
849 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
854 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
857 // --------------------------------------------------------------------------------
860 // --------------------------------------------------------------------------------
862 // extractByIndex($p_index, $p_path="./", $p_remove_path="")
863 // extractByIndex($p_index, [$p_option, $p_option_value, ...])
865 // This method supports two synopsis. The first one is historical.
866 // This method is doing a partial extract of the archive.
867 // The extracted files or folders are identified by their index in the
868 // archive (from 0 to n).
869 // Note that if the index identify a folder, only the folder entry is
870 // extracted, not all the files included in the archive.
872 // $p_index : A single index (integer) or a string of indexes of files to
873 // extract. The form of the string is "0,4-6,8-12" with only numbers
874 // and '-' for range or ',' to separate ranges. No spaces or ';'
876 // $p_path : Path where the files and directories are to be extracted
877 // $p_remove_path : First part ('root' part) of the memorized path
878 // (if any similar) to remove while extracting.
881 // PCLZIP_OPT_ADD_PATH :
882 // PCLZIP_OPT_REMOVE_PATH :
883 // PCLZIP_OPT_REMOVE_ALL_PATH :
884 // PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
886 // The resulting content is in a new field 'content' in the file
888 // This option must be used alone (any other options are ignored).
889 // PCLZIP_CB_PRE_EXTRACT :
890 // PCLZIP_CB_POST_EXTRACT :
893 // The list of the extracted files, with a status of the action.
894 // (see PclZip::listContent() for list entry format)
895 // --------------------------------------------------------------------------------
896 //function extractByIndex($p_index, options...)
897 function extractByIndex($p_index)
899 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extractByIndex", "index='$p_index', ...");
902 // ----- Reset the error handler
903 $this->privErrorReset();
905 // ----- Check archive
906 if (!$this->privCheckFormat()) {
907 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
911 // ----- Set default values
912 $v_options = array();
916 $v_remove_all_path = false;
918 // ----- Look for variable options arguments
919 $v_size = func_num_args();
920 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
922 // ----- Default values for option
923 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
925 // ----- Look for arguments
927 // ----- Get the arguments
928 $v_arg_list = func_get_args();
930 // ----- Remove form the options list the first argument
931 array_shift($v_arg_list);
934 // ----- Look for first arg
935 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
936 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
938 // ----- Parse the options
939 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
940 array (PCLZIP_OPT_PATH => 'optional',
941 PCLZIP_OPT_REMOVE_PATH => 'optional',
942 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
943 PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
944 PCLZIP_OPT_ADD_PATH => 'optional',
945 PCLZIP_CB_PRE_EXTRACT => 'optional',
946 PCLZIP_CB_POST_EXTRACT => 'optional',
947 PCLZIP_OPT_SET_CHMOD => 'optional',
948 PCLZIP_OPT_REPLACE_NEWER => 'optional'
949 ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
950 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
951 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
952 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
953 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
955 if ($v_result != 1) {
956 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
960 // ----- Set the arguments
961 if (isset($v_options[PCLZIP_OPT_PATH])) {
962 $v_path = $v_options[PCLZIP_OPT_PATH];
964 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
965 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
967 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
968 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
970 if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
971 // ----- Check for '/' in last path char
972 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
975 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
977 if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
978 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
979 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING not set.");
982 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING set.");
986 // ----- Look for 2 args
987 // Here we need to support the first historic synopsis of the
990 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
992 // ----- Get the first argument
993 $v_path = $v_arg_list[0];
995 // ----- Look for the optional second argument
997 $v_remove_path = $v_arg_list[1];
999 else if ($v_size > 2) {
1001 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
1004 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1011 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "index='$p_index', path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
1014 // Here I want to reuse extractByRule(), so I need to parse the $p_index
1015 // with privParseOptions()
1016 $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
1017 $v_options_trick = array();
1018 $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
1019 array (PCLZIP_OPT_BY_INDEX => 'optional' ));
1020 if ($v_result != 1) {
1021 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1024 $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
1026 // ----- Look for default option values
1027 $this->privOptionDefaultThreshold($v_options);
1029 // ----- Call the extracting fct
1030 if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
1031 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
1036 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
1039 // --------------------------------------------------------------------------------
1041 // --------------------------------------------------------------------------------
1043 // delete([$p_option, $p_option_value, ...])
1045 // This method removes files from the archive.
1046 // If no parameters are given, then all the archive is emptied.
1048 // None or optional arguments.
1050 // PCLZIP_OPT_BY_INDEX :
1051 // PCLZIP_OPT_BY_NAME :
1052 // PCLZIP_OPT_BY_EREG :
1053 // PCLZIP_OPT_BY_PREG :
1056 // The list of the files which are still present in the archive.
1057 // (see PclZip::listContent() for list entry format)
1058 // --------------------------------------------------------------------------------
1061 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::delete", "");
1064 // ----- Reset the error handler
1065 $this->privErrorReset();
1067 // ----- Check archive
1068 if (!$this->privCheckFormat()) {
1069 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1073 // ----- Set default values
1074 $v_options = array();
1076 // ----- Look for variable options arguments
1077 $v_size = func_num_args();
1078 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
1080 // ----- Look for arguments
1082 // ----- Get the arguments
1083 $v_arg_list = func_get_args();
1085 // ----- Parse the options
1086 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
1087 array (PCLZIP_OPT_BY_NAME => 'optional',
1088 PCLZIP_OPT_BY_EREG => 'optional',
1089 PCLZIP_OPT_BY_PREG => 'optional',
1090 PCLZIP_OPT_BY_INDEX => 'optional' ));
1091 if ($v_result != 1) {
1092 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1097 // ----- Magic quotes trick
1098 $this->privDisableMagicQuotes();
1100 // ----- Call the delete fct
1102 if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
1103 $this->privSwapBackMagicQuotes();
1105 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
1109 // ----- Magic quotes trick
1110 $this->privSwapBackMagicQuotes();
1113 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_list);
1116 // --------------------------------------------------------------------------------
1118 // --------------------------------------------------------------------------------
1119 // Function : deleteByIndex()
1121 // ***** Deprecated *****
1122 // delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
1123 // --------------------------------------------------------------------------------
1124 function deleteByIndex($p_index)
1126 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::deleteByIndex", "index='$p_index'");
1128 $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
1131 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
1134 // --------------------------------------------------------------------------------
1136 // --------------------------------------------------------------------------------
1137 // Function : properties()
1139 // This method gives the properties of the archive.
1140 // The properties are :
1141 // nb : Number of files in the archive
1142 // comment : Comment associated with the archive file
1143 // status : not_exist, ok
1148 // An array with the archive properties.
1149 // --------------------------------------------------------------------------------
1150 function properties()
1152 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::properties", "");
1154 // ----- Reset the error handler
1155 $this->privErrorReset();
1157 // ----- Magic quotes trick
1158 $this->privDisableMagicQuotes();
1160 // ----- Check archive
1161 if (!$this->privCheckFormat()) {
1162 $this->privSwapBackMagicQuotes();
1163 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1167 // ----- Default properties
1169 $v_prop['comment'] = '';
1171 $v_prop['status'] = 'not_exist';
1173 // ----- Look if file exists
1174 if (@is_file($this->zipname))
1176 // ----- Open the zip file
1177 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
1178 if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
1180 $this->privSwapBackMagicQuotes();
1183 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
1186 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), 0);
1190 // ----- Read the central directory informations
1191 $v_central_dir = array();
1192 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
1194 $this->privSwapBackMagicQuotes();
1195 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1199 // ----- Close the zip file
1200 $this->privCloseFd();
1202 // ----- Set the user attributes
1203 $v_prop['comment'] = $v_central_dir['comment'];
1204 $v_prop['nb'] = $v_central_dir['entries'];
1205 $v_prop['status'] = 'ok';
1208 // ----- Magic quotes trick
1209 $this->privSwapBackMagicQuotes();
1212 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_prop);
1215 // --------------------------------------------------------------------------------
1217 // --------------------------------------------------------------------------------
1218 // Function : duplicate()
1220 // This method creates an archive by copying the content of an other one. If
1221 // the archive already exist, it is replaced by the new one without any warning.
1223 // $p_archive : The filename of a valid archive, or
1224 // a valid PclZip object.
1227 // 0 or a negative value on error (error code).
1228 // --------------------------------------------------------------------------------
1229 function duplicate($p_archive)
1231 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::duplicate", "");
1234 // ----- Reset the error handler
1235 $this->privErrorReset();
1237 // ----- Look if the $p_archive is a PclZip object
1238 if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
1240 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is valid PclZip object '".$p_archive->zipname."'");
1242 // ----- Duplicate the archive
1243 $v_result = $this->privDuplicate($p_archive->zipname);
1246 // ----- Look if the $p_archive is a string (so a filename)
1247 else if (is_string($p_archive))
1249 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is a filename '$p_archive'");
1251 // ----- Check that $p_archive is a valid zip file
1252 // TBC : Should also check the archive format
1253 if (!is_file($p_archive)) {
1255 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
1256 $v_result = PCLZIP_ERR_MISSING_FILE;
1259 // ----- Duplicate the archive
1260 $v_result = $this->privDuplicate($p_archive);
1264 // ----- Invalid variable
1268 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1269 $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1273 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1276 // --------------------------------------------------------------------------------
1278 // --------------------------------------------------------------------------------
1279 // Function : merge()
1281 // This method merge the $p_archive_to_add archive at the end of the current
1283 // If the archive ($this) does not exist, the merge becomes a duplicate.
1284 // If the $p_archive_to_add archive does not exist, the merge is a success.
1286 // $p_archive_to_add : It can be directly the filename of a valid zip archive,
1287 // or a PclZip object archive.
1290 // 0 or negative values on error (see below).
1291 // --------------------------------------------------------------------------------
1292 function merge($p_archive_to_add)
1294 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::merge", "");
1297 // ----- Reset the error handler
1298 $this->privErrorReset();
1300 // ----- Check archive
1301 if (!$this->privCheckFormat()) {
1302 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1306 // ----- Look if the $p_archive_to_add is a PclZip object
1307 if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
1309 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is valid PclZip object");
1311 // ----- Merge the archive
1312 $v_result = $this->privMerge($p_archive_to_add);
1315 // ----- Look if the $p_archive_to_add is a string (so a filename)
1316 else if (is_string($p_archive_to_add))
1318 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is a filename");
1320 // ----- Create a temporary archive
1321 $v_object_archive = new PclZip($p_archive_to_add);
1323 // ----- Merge the archive
1324 $v_result = $this->privMerge($v_object_archive);
1327 // ----- Invalid variable
1331 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1332 $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1336 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1339 // --------------------------------------------------------------------------------
1343 // --------------------------------------------------------------------------------
1344 // Function : errorCode()
1347 // --------------------------------------------------------------------------------
1348 function errorCode()
1350 if (PCLZIP_ERROR_EXTERNAL == 1) {
1351 return(PclErrorCode());
1354 return($this->error_code);
1357 // --------------------------------------------------------------------------------
1359 // --------------------------------------------------------------------------------
1360 // Function : errorName()
1363 // --------------------------------------------------------------------------------
1364 function errorName($p_with_code=false)
1366 $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
1367 PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
1368 PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
1369 PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
1370 PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
1371 PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
1372 PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
1373 PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
1374 PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
1375 PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
1376 PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
1377 PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
1378 PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
1379 PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
1380 PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
1381 PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
1382 PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
1383 PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
1384 PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
1385 ,PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
1386 ,PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
1389 if (isset($v_name[$this->error_code])) {
1390 $v_value = $v_name[$this->error_code];
1393 $v_value = 'NoName';
1397 return($v_value.' ('.$this->error_code.')');
1403 // --------------------------------------------------------------------------------
1405 // --------------------------------------------------------------------------------
1406 // Function : errorInfo()
1409 // --------------------------------------------------------------------------------
1410 function errorInfo($p_full=false)
1412 if (PCLZIP_ERROR_EXTERNAL == 1) {
1413 return(PclErrorString());
1417 return($this->errorName(true)." : ".$this->error_string);
1420 return($this->error_string." [code ".$this->error_code."]");
1424 // --------------------------------------------------------------------------------
1427 // --------------------------------------------------------------------------------
1428 // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1430 // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
1431 // --------------------------------------------------------------------------------
1435 // --------------------------------------------------------------------------------
1436 // Function : privCheckFormat()
1438 // This method check that the archive exists and is a valid zip archive.
1439 // Several level of check exists. (futur)
1441 // $p_level : Level of check. Default 0.
1442 // 0 : Check the first bytes (magic codes) (default value))
1443 // 1 : 0 + Check the central directory (futur)
1444 // 2 : 1 + Check each file header (futur)
1447 // false on error, the error code is set.
1448 // --------------------------------------------------------------------------------
1449 function privCheckFormat($p_level=0)
1451 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFormat", "");
1454 // ----- Reset the file system cache
1457 // ----- Reset the error handler
1458 $this->privErrorReset();
1460 // ----- Look if the file exits
1461 if (!is_file($this->zipname)) {
1463 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
1464 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1468 // ----- Check that the file is readeable
1469 if (!is_readable($this->zipname)) {
1471 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
1472 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1476 // ----- Check the magic code
1479 // ----- Check the central header
1482 // ----- Check each file header
1486 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1489 // --------------------------------------------------------------------------------
1491 // --------------------------------------------------------------------------------
1492 // Function : privParseOptions()
1494 // This internal methods reads the variable list of arguments ($p_options_list,
1495 // $p_size) and generate an array with the options and values ($v_result_list).
1496 // $v_requested_options contains the options that can be present and those that
1498 // $v_requested_options is an array, with the option value as key, and 'optional',
1499 // or 'mandatory' as value.
1505 // --------------------------------------------------------------------------------
1506 function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
1508 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privParseOptions", "");
1511 // ----- Read the options
1513 while ($i<$p_size) {
1514 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Looking for table index $i, option = '".PclZipUtilOptionText($p_options_list[$i])."(".$p_options_list[$i].")'");
1516 // ----- Check if the option is supported
1517 if (!isset($v_requested_options[$p_options_list[$i]])) {
1519 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
1522 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1523 return PclZip::errorCode();
1526 // ----- Look for next option
1527 switch ($p_options_list[$i]) {
1528 // ----- Look for options that request a path value
1529 case PCLZIP_OPT_PATH :
1530 case PCLZIP_OPT_REMOVE_PATH :
1531 case PCLZIP_OPT_ADD_PATH :
1532 // ----- Check the number of parameters
1533 if (($i+1) >= $p_size) {
1535 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1538 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1539 return PclZip::errorCode();
1542 // ----- Get the value
1543 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
1544 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1548 case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
1549 // ----- Check the number of parameters
1550 if (($i+1) >= $p_size) {
1551 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1552 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1553 return PclZip::errorCode();
1556 // ----- Check for incompatible options
1557 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
1558 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1559 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1560 return PclZip::errorCode();
1563 // ----- Check the value
1564 $v_value = $p_options_list[$i+1];
1565 if ((!is_integer($v_value)) || ($v_value<0)) {
1566 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1567 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1568 return PclZip::errorCode();
1571 // ----- Get the value (and convert it in bytes)
1572 $v_result_list[$p_options_list[$i]] = $v_value*1048576;
1573 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1577 case PCLZIP_OPT_TEMP_FILE_ON :
1578 // ----- Check for incompatible options
1579 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
1580 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1581 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1582 return PclZip::errorCode();
1585 $v_result_list[$p_options_list[$i]] = true;
1586 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1589 case PCLZIP_OPT_TEMP_FILE_OFF :
1590 // ----- Check for incompatible options
1591 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
1592 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
1593 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1594 return PclZip::errorCode();
1596 // ----- Check for incompatible options
1597 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
1598 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
1599 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1600 return PclZip::errorCode();
1603 $v_result_list[$p_options_list[$i]] = true;
1604 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1607 case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
1608 // ----- Check the number of parameters
1609 if (($i+1) >= $p_size) {
1611 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1614 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1615 return PclZip::errorCode();
1618 // ----- Get the value
1619 if ( is_string($p_options_list[$i+1])
1620 && ($p_options_list[$i+1] != '')) {
1621 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
1622 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1626 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." set with an empty value is ignored.");
1630 // ----- Look for options that request an array of string for value
1631 case PCLZIP_OPT_BY_NAME :
1632 // ----- Check the number of parameters
1633 if (($i+1) >= $p_size) {
1635 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1638 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1639 return PclZip::errorCode();
1642 // ----- Get the value
1643 if (is_string($p_options_list[$i+1])) {
1644 $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
1646 else if (is_array($p_options_list[$i+1])) {
1647 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1651 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1654 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1655 return PclZip::errorCode();
1657 ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1661 // ----- Look for options that request an EREG or PREG expression
1662 case PCLZIP_OPT_BY_EREG :
1663 case PCLZIP_OPT_BY_PREG :
1664 //case PCLZIP_OPT_CRYPT :
1665 // ----- Check the number of parameters
1666 if (($i+1) >= $p_size) {
1668 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1671 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1672 return PclZip::errorCode();
1675 // ----- Get the value
1676 if (is_string($p_options_list[$i+1])) {
1677 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1681 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1684 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1685 return PclZip::errorCode();
1687 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1691 // ----- Look for options that takes a string
1692 case PCLZIP_OPT_COMMENT :
1693 case PCLZIP_OPT_ADD_COMMENT :
1694 case PCLZIP_OPT_PREPEND_COMMENT :
1695 // ----- Check the number of parameters
1696 if (($i+1) >= $p_size) {
1698 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
1699 "Missing parameter value for option '"
1700 .PclZipUtilOptionText($p_options_list[$i])
1704 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1705 return PclZip::errorCode();
1708 // ----- Get the value
1709 if (is_string($p_options_list[$i+1])) {
1710 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1714 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
1715 "Wrong parameter value for option '"
1716 .PclZipUtilOptionText($p_options_list[$i])
1720 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1721 return PclZip::errorCode();
1723 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1727 // ----- Look for options that request an array of index
1728 case PCLZIP_OPT_BY_INDEX :
1729 // ----- Check the number of parameters
1730 if (($i+1) >= $p_size) {
1732 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1735 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1736 return PclZip::errorCode();
1739 // ----- Get the value
1740 $v_work_list = array();
1741 if (is_string($p_options_list[$i+1])) {
1742 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is a string '".$p_options_list[$i+1]."'");
1744 // ----- Remove spaces
1745 $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
1747 // ----- Parse items
1748 $v_work_list = explode(",", $p_options_list[$i+1]);
1750 else if (is_integer($p_options_list[$i+1])) {
1751 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an integer '".$p_options_list[$i+1]."'");
1752 $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
1754 else if (is_array($p_options_list[$i+1])) {
1755 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an array");
1756 $v_work_list = $p_options_list[$i+1];
1760 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1763 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1764 return PclZip::errorCode();
1767 // ----- Reduce the index list
1768 // each index item in the list must be a couple with a start and
1769 // an end value : [0,3], [5-5], [8-10], ...
1770 // ----- Check the format of each item
1773 for ($j=0; $j<sizeof($v_work_list); $j++) {
1774 // ----- Explode the item
1775 $v_item_list = explode("-", $v_work_list[$j]);
1776 $v_size_item_list = sizeof($v_item_list);
1778 // ----- TBC : Here we might check that each item is a
1781 // ----- Look for single value
1782 if ($v_size_item_list == 1) {
1783 // ----- Set the option value
1784 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1785 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
1787 elseif ($v_size_item_list == 2) {
1788 // ----- Set the option value
1789 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1790 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
1794 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1797 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1798 return PclZip::errorCode();
1801 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extracted index item = [".$v_result_list[$p_options_list[$i]][$j]['start'].",".$v_result_list[$p_options_list[$i]][$j]['end']."]");
1803 // ----- Look for list sort
1804 if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
1805 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The list should be sorted ...");
1808 // ----- TBC : An automatic sort should be writen ...
1810 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1813 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1814 return PclZip::errorCode();
1816 $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
1819 // ----- Sort the items
1821 // TBC : To Be Completed
1822 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "List sorting is not yet write ...");
1825 // ----- Next option
1829 // ----- Look for options that request no value
1830 case PCLZIP_OPT_REMOVE_ALL_PATH :
1831 case PCLZIP_OPT_EXTRACT_AS_STRING :
1832 case PCLZIP_OPT_NO_COMPRESSION :
1833 case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
1834 case PCLZIP_OPT_REPLACE_NEWER :
1835 case PCLZIP_OPT_STOP_ON_ERROR :
1836 $v_result_list[$p_options_list[$i]] = true;
1837 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1840 // ----- Look for options that request an octal value
1841 case PCLZIP_OPT_SET_CHMOD :
1842 // ----- Check the number of parameters
1843 if (($i+1) >= $p_size) {
1845 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1848 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1849 return PclZip::errorCode();
1852 // ----- Get the value
1853 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1854 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1858 // ----- Look for options that request a call-back
1859 case PCLZIP_CB_PRE_EXTRACT :
1860 case PCLZIP_CB_POST_EXTRACT :
1861 case PCLZIP_CB_PRE_ADD :
1862 case PCLZIP_CB_POST_ADD :
1864 case PCLZIP_CB_PRE_DELETE :
1865 case PCLZIP_CB_POST_DELETE :
1866 case PCLZIP_CB_PRE_LIST :
1867 case PCLZIP_CB_POST_LIST :
1869 // ----- Check the number of parameters
1870 if (($i+1) >= $p_size) {
1872 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1875 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1876 return PclZip::errorCode();
1879 // ----- Get the value
1880 $v_function_name = $p_options_list[$i+1];
1881 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "call-back ".PclZipUtilOptionText($p_options_list[$i])." = '".$v_function_name."'");
1883 // ----- Check that the value is a valid existing function
1884 if (!function_exists($v_function_name)) {
1886 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1889 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1890 return PclZip::errorCode();
1893 // ----- Set the attribute
1894 $v_result_list[$p_options_list[$i]] = $v_function_name;
1900 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
1901 "Unknown parameter '"
1902 .$p_options_list[$i]."'");
1905 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1906 return PclZip::errorCode();
1909 // ----- Next options
1913 // ----- Look for mandatory options
1914 if ($v_requested_options !== false) {
1915 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
1916 // ----- Look for mandatory option
1917 if ($v_requested_options[$key] == 'mandatory') {
1918 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
1919 // ----- Look if present
1920 if (!isset($v_result_list[$key])) {
1922 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
1925 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1926 return PclZip::errorCode();
1932 // ----- Look for default values
1933 if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
1934 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Calculate auto threshold");
1939 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1942 // --------------------------------------------------------------------------------
1944 // --------------------------------------------------------------------------------
1945 // Function : privOptionDefaultThreshold()
1949 // --------------------------------------------------------------------------------
1950 function privOptionDefaultThreshold(&$p_options)
1952 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privOptionDefaultThreshold", "");
1955 if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
1956 || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
1957 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1961 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Create an auto-threshold for use of temporay files");
1962 // ----- Get 'memory_limit' configuration value
1963 $v_memory_limit = ini_get('memory_limit');
1964 $v_memory_limit = trim($v_memory_limit);
1965 $last = strtolower(substr($v_memory_limit, -1));
1968 //$v_memory_limit = $v_memory_limit*1024*1024*1024;
1969 $v_memory_limit = $v_memory_limit*1073741824;
1971 //$v_memory_limit = $v_memory_limit*1024*1024;
1972 $v_memory_limit = $v_memory_limit*1048576;
1974 $v_memory_limit = $v_memory_limit*1024;
1976 $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit*PCLZIP_TEMPORARY_FILE_RATIO);
1978 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Current memory usage : ".memory_get_usage(TRUE)." bytes");
1979 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Threshold value is : ".$p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]." bytes");
1981 // ----- Sanity check : No threshold if value lower than 1M
1982 if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
1983 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Unset the threshold (value ".$p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD].") because under 1Mo sanity check)");
1984 unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
1988 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1991 // --------------------------------------------------------------------------------
1993 // --------------------------------------------------------------------------------
1994 // Function : privFileDescrParseAtt()
2000 // --------------------------------------------------------------------------------
2001 function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
2003 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrParseAtt", "");
2006 // ----- For each file in the list check the attributes
2007 foreach ($p_file_list as $v_key => $v_value) {
2009 // ----- Check if the option is supported
2010 if (!isset($v_requested_options[$v_key])) {
2012 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
2015 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2016 return PclZip::errorCode();
2019 // ----- Look for attribute
2021 case PCLZIP_ATT_FILE_NAME :
2022 if (!is_string($v_value)) {
2023 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
2024 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2025 return PclZip::errorCode();
2028 $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
2029 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2031 if ($p_filedescr['filename'] == '') {
2032 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
2033 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2034 return PclZip::errorCode();
2039 case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
2040 if (!is_string($v_value)) {
2041 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
2042 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2043 return PclZip::errorCode();
2046 $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
2047 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2049 if ($p_filedescr['new_short_name'] == '') {
2050 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
2051 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2052 return PclZip::errorCode();
2056 case PCLZIP_ATT_FILE_NEW_FULL_NAME :
2057 if (!is_string($v_value)) {
2058 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
2059 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2060 return PclZip::errorCode();
2063 $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
2064 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2066 if ($p_filedescr['new_full_name'] == '') {
2067 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
2068 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2069 return PclZip::errorCode();
2073 // ----- Look for options that takes a string
2074 case PCLZIP_ATT_FILE_COMMENT :
2075 if (!is_string($v_value)) {
2076 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
2077 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2078 return PclZip::errorCode();
2081 $p_filedescr['comment'] = $v_value;
2082 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2085 case PCLZIP_ATT_FILE_MTIME :
2086 if (!is_integer($v_value)) {
2087 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
2088 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2089 return PclZip::errorCode();
2092 $p_filedescr['mtime'] = $v_value;
2093 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2096 case PCLZIP_ATT_FILE_CONTENT :
2097 $p_filedescr['content'] = $v_value;
2098 ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2103 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
2104 "Unknown parameter '".$v_key."'");
2107 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2108 return PclZip::errorCode();
2111 // ----- Look for mandatory options
2112 if ($v_requested_options !== false) {
2113 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
2114 // ----- Look for mandatory option
2115 if ($v_requested_options[$key] == 'mandatory') {
2116 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
2117 // ----- Look if present
2118 if (!isset($p_file_list[$key])) {
2119 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
2120 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2121 return PclZip::errorCode();
2131 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2134 // --------------------------------------------------------------------------------
2136 // --------------------------------------------------------------------------------
2137 // Function : privFileDescrExpand()
2139 // This method look for each item of the list to see if its a file, a folder
2140 // or a string to be added as file. For any other type of files (link, other)
2141 // just ignore the item.
2142 // Then prepare the information that will be stored for that file.
2143 // When its a folder, expand the folder with all the files that are in that
2144 // folder (recursively).
2149 // --------------------------------------------------------------------------------
2150 function privFileDescrExpand(&$p_filedescr_list, &$p_options)
2152 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrExpand", "");
2155 // ----- Create a result list
2156 $v_result_list = array();
2158 // ----- Look each entry
2159 for ($i=0; $i<sizeof($p_filedescr_list); $i++) {
2160 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Looking for file ".$i.".");
2162 // ----- Get filedescr
2163 $v_descr = $p_filedescr_list[$i];
2165 // ----- Reduce the filename
2166 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filedescr before reduction :'".$v_descr['filename']."'");
2167 $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
2168 $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
2169 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filedescr after reduction :'".$v_descr['filename']."'");
2171 // ----- Look for real file or folder
2172 if (file_exists($v_descr['filename'])) {
2173 if (@is_file($v_descr['filename'])) {
2174 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a file");
2175 $v_descr['type'] = 'file';
2177 else if (@is_dir($v_descr['filename'])) {
2178 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a folder");
2179 $v_descr['type'] = 'folder';
2181 else if (@is_link($v_descr['filename'])) {
2182 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Unsupported file type : link");
2187 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Unsupported file type : unknown type");
2193 // ----- Look for string added as file
2194 else if (isset($v_descr['content'])) {
2195 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a string added as a file");
2196 $v_descr['type'] = 'virtual_file';
2199 // ----- Missing file
2202 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_descr['filename']."' does not exist");
2203 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$v_descr['filename']."' does not exist");
2206 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2207 return PclZip::errorCode();
2210 // ----- Calculate the stored filename
2211 $this->privCalculateStoredFilename($v_descr, $p_options);
2213 // ----- Add the descriptor in result list
2214 $v_result_list[sizeof($v_result_list)] = $v_descr;
2216 // ----- Look for folder
2217 if ($v_descr['type'] == 'folder') {
2218 // ----- List of items in folder
2219 $v_dirlist_descr = array();
2221 if ($v_folder_handler = @opendir($v_descr['filename'])) {
2222 while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
2223 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for '".$v_item_handler."' in the directory");
2225 // ----- Skip '.' and '..'
2226 if (($v_item_handler == '.') || ($v_item_handler == '..')) {
2230 // ----- Compose the full filename
2231 $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
2233 // ----- Look for different stored filename
2234 // Because the name of the folder was changed, the name of the
2235 // files/sub-folders also change
2236 if ($v_descr['stored_filename'] != $v_descr['filename']) {
2237 if ($v_descr['stored_filename'] != '') {
2238 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
2241 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
2248 @closedir($v_folder_handler);
2251 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to open dir '".$v_descr['filename']."' in read mode. Skipped.");
2252 // TBC : unable to open folder in read mode
2255 // ----- Expand each element of the list
2256 if ($v_dirlist_nb != 0) {
2258 if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
2259 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2263 // ----- Concat the resulting list
2264 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Merging result list (size '".sizeof($v_result_list)."') with dirlist (size '".sizeof($v_dirlist_descr)."')");
2265 $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
2266 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "merged result list is size '".sizeof($v_result_list)."'");
2269 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Nothing in this folder to expand.");
2272 // ----- Free local array
2273 unset($v_dirlist_descr);
2277 // ----- Get the result list
2278 $p_filedescr_list = $v_result_list;
2281 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2284 // --------------------------------------------------------------------------------
2286 // --------------------------------------------------------------------------------
2287 // Function : privCreate()
2291 // --------------------------------------------------------------------------------
2292 function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
2294 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCreate", "list");
2296 $v_list_detail = array();
2298 // ----- Magic quotes trick
2299 $this->privDisableMagicQuotes();
2301 // ----- Open the file in write mode
2302 if (($v_result = $this->privOpenFd('wb')) != 1)
2305 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2309 // ----- Add the list of files
2310 $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
2313 $this->privCloseFd();
2315 // ----- Magic quotes trick
2316 $this->privSwapBackMagicQuotes();
2319 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2322 // --------------------------------------------------------------------------------
2324 // --------------------------------------------------------------------------------
2325 // Function : privAdd()
2329 // --------------------------------------------------------------------------------
2330 function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
2332 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAdd", "list");
2334 $v_list_detail = array();
2336 // ----- Look if the archive exists or is empty
2337 if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
2339 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, or is empty, create it.");
2341 // ----- Do a create
2342 $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
2345 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2348 // ----- Magic quotes trick
2349 $this->privDisableMagicQuotes();
2351 // ----- Open the zip file
2352 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
2353 if (($v_result=$this->privOpenFd('rb')) != 1)
2355 // ----- Magic quotes trick
2356 $this->privSwapBackMagicQuotes();
2359 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2363 // ----- Read the central directory informations
2364 $v_central_dir = array();
2365 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
2367 $this->privCloseFd();
2368 $this->privSwapBackMagicQuotes();
2369 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2373 // ----- Go to beginning of File
2374 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
2375 @rewind($this->zip_fd);
2376 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
2378 // ----- Creates a temporay file
2379 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
2381 // ----- Open the temporary file in write mode
2382 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
2383 if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
2385 $this->privCloseFd();
2386 $this->privSwapBackMagicQuotes();
2388 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
2391 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2392 return PclZip::errorCode();
2395 // ----- Copy the files from the archive to the temporary file
2396 // TBC : Here I should better append the file and go back to erase the central dir
2397 $v_size = $v_central_dir['offset'];
2398 while ($v_size != 0)
2400 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2401 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
2402 $v_buffer = fread($this->zip_fd, $v_read_size);
2403 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
2404 $v_size -= $v_read_size;
2407 // ----- Swap the file descriptor
2408 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
2409 // the following methods on the temporary fil and not the real archive
2410 $v_swap = $this->zip_fd;
2411 $this->zip_fd = $v_zip_temp_fd;
2412 $v_zip_temp_fd = $v_swap;
2414 // ----- Add the files
2415 $v_header_list = array();
2416 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2418 fclose($v_zip_temp_fd);
2419 $this->privCloseFd();
2420 @unlink($v_zip_temp_name);
2421 $this->privSwapBackMagicQuotes();
2424 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2428 // ----- Store the offset of the central dir
2429 $v_offset = @ftell($this->zip_fd);
2430 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
2432 // ----- Copy the block of file headers from the old archive
2433 $v_size = $v_central_dir['size'];
2434 while ($v_size != 0)
2436 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2437 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
2438 $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
2439 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
2440 $v_size -= $v_read_size;
2443 // ----- Create the Central Dir files header
2444 for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++)
2446 // ----- Create the file header
2447 if ($v_header_list[$i]['status'] == 'ok') {
2448 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2449 fclose($v_zip_temp_fd);
2450 $this->privCloseFd();
2451 @unlink($v_zip_temp_name);
2452 $this->privSwapBackMagicQuotes();
2455 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2461 // ----- Transform the header to a 'usable' info
2462 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2465 // ----- Zip file comment
2466 $v_comment = $v_central_dir['comment'];
2467 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2468 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2470 if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
2471 $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
2473 if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
2474 $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
2477 // ----- Calculate the size of the central header
2478 $v_size = @ftell($this->zip_fd)-$v_offset;
2480 // ----- Create the central dir footer
2481 if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
2483 // ----- Reset the file list
2484 unset($v_header_list);
2485 $this->privSwapBackMagicQuotes();
2488 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2492 // ----- Swap back the file descriptor
2493 $v_swap = $this->zip_fd;
2494 $this->zip_fd = $v_zip_temp_fd;
2495 $v_zip_temp_fd = $v_swap;
2498 $this->privCloseFd();
2500 // ----- Close the temporary file
2501 @fclose($v_zip_temp_fd);
2503 // ----- Magic quotes trick
2504 $this->privSwapBackMagicQuotes();
2506 // ----- Delete the zip file
2507 // TBC : I should test the result ...
2508 @unlink($this->zipname);
2510 // ----- Rename the temporary file
2511 // TBC : I should test the result ...
2512 //@rename($v_zip_temp_name, $this->zipname);
2513 PclZipUtilRename($v_zip_temp_name, $this->zipname);
2516 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2519 // --------------------------------------------------------------------------------
2521 // --------------------------------------------------------------------------------
2522 // Function : privOpenFd()
2525 // --------------------------------------------------------------------------------
2526 function privOpenFd($p_mode)
2528 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privOpenFd", 'mode='.$p_mode);
2531 // ----- Look if already open
2532 if ($this->zip_fd != 0)
2535 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
2538 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2539 return PclZip::errorCode();
2542 // ----- Open the zip file
2543 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Open file in '.$p_mode.' mode');
2544 if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
2547 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
2550 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2551 return PclZip::errorCode();
2555 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2558 // --------------------------------------------------------------------------------
2560 // --------------------------------------------------------------------------------
2561 // Function : privCloseFd()
2564 // --------------------------------------------------------------------------------
2565 function privCloseFd()
2567 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCloseFd", "");
2570 if ($this->zip_fd != 0)
2571 @fclose($this->zip_fd);
2575 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2578 // --------------------------------------------------------------------------------
2580 // --------------------------------------------------------------------------------
2581 // Function : privAddList()
2583 // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
2584 // different from the real path of the file. This is usefull if you want to have PclTar
2585 // running in any directory, and memorize relative path from an other directory.
2587 // $p_list : An array containing the file or directory names to add in the tar
2588 // $p_result_list : list of added files with their properties (specially the status field)
2589 // $p_add_dir : Path to add in the filename path archived
2590 // $p_remove_dir : Path to remove in the filename path archived
2592 // --------------------------------------------------------------------------------
2593 // function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
2594 function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
2596 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddList", "list");
2599 // ----- Add the files
2600 $v_header_list = array();
2601 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2604 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2608 // ----- Store the offset of the central dir
2609 $v_offset = @ftell($this->zip_fd);
2611 // ----- Create the Central Dir files header
2612 for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
2614 // ----- Create the file header
2615 if ($v_header_list[$i]['status'] == 'ok') {
2616 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2618 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2624 // ----- Transform the header to a 'usable' info
2625 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2628 // ----- Zip file comment
2630 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2631 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2634 // ----- Calculate the size of the central header
2635 $v_size = @ftell($this->zip_fd)-$v_offset;
2637 // ----- Create the central dir footer
2638 if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
2640 // ----- Reset the file list
2641 unset($v_header_list);
2644 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2649 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2652 // --------------------------------------------------------------------------------
2654 // --------------------------------------------------------------------------------
2655 // Function : privAddFileList()
2658 // $p_filedescr_list : An array containing the file description
2659 // or directory names to add in the zip
2660 // $p_result_list : list of added files with their properties (specially the status field)
2662 // --------------------------------------------------------------------------------
2663 function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
2665 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFileList", "filedescr_list");
2667 $v_header = array();
2669 // ----- Recuperate the current number of elt in list
2670 $v_nb = sizeof($p_result_list);
2671 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Before add, list have ".$v_nb." elements");
2673 // ----- Loop on the files
2674 for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++) {
2675 // ----- Format the filename
2676 $p_filedescr_list[$j]['filename']
2677 = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
2679 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for file '".$p_filedescr_list[$j]['filename']."'");
2681 // ----- Skip empty file names
2682 // TBC : Can this be possible ? not checked in DescrParseAtt ?
2683 if ($p_filedescr_list[$j]['filename'] == "") {
2684 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Skip empty filename");
2688 // ----- Check the filename
2689 if ( ($p_filedescr_list[$j]['type'] != 'virtual_file')
2690 && (!file_exists($p_filedescr_list[$j]['filename']))) {
2691 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
2692 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
2693 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2694 return PclZip::errorCode();
2697 // ----- Look if it is a file or a dir with no all path remove option
2698 // or a dir with all its path removed
2699 // if ( (is_file($p_filedescr_list[$j]['filename']))
2700 // || ( is_dir($p_filedescr_list[$j]['filename'])
2701 if ( ($p_filedescr_list[$j]['type'] == 'file')
2702 || ($p_filedescr_list[$j]['type'] == 'virtual_file')
2703 || ( ($p_filedescr_list[$j]['type'] == 'folder')
2704 && ( !isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])
2705 || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))
2708 // ----- Add the file
2709 $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
2711 if ($v_result != 1) {
2712 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2716 // ----- Store the file infos
2717 $p_result_list[$v_nb++] = $v_header;
2720 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "After add, list have ".$v_nb." elements");
2723 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2726 // --------------------------------------------------------------------------------
2728 // --------------------------------------------------------------------------------
2729 // Function : privAddFile()
2733 // --------------------------------------------------------------------------------
2734 function privAddFile($p_filedescr, &$p_header, &$p_options)
2736 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFile", "filename='".$p_filedescr['filename']."'");
2739 // ----- Working variable
2740 $p_filename = $p_filedescr['filename'];
2742 // TBC : Already done in the fileAtt check ... ?
2743 if ($p_filename == "") {
2745 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
2748 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2749 return PclZip::errorCode();
2752 // ----- Look for a stored different filename
2754 if (isset($p_filedescr['stored_filename'])) {
2755 $v_stored_filename = $p_filedescr['stored_filename'];
2756 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is NOT the same "'.$v_stored_filename.'"');
2759 $v_stored_filename = $p_filedescr['stored_filename'];
2760 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is the same');
2764 // ----- Set the file properties
2766 $p_header['version'] = 20;
2767 $p_header['version_extracted'] = 10;
2768 $p_header['flag'] = 0;
2769 $p_header['compression'] = 0;
2770 $p_header['crc'] = 0;
2771 $p_header['compressed_size'] = 0;
2772 $p_header['filename_len'] = strlen($p_filename);
2773 $p_header['extra_len'] = 0;
2774 $p_header['disk'] = 0;
2775 $p_header['internal'] = 0;
2776 $p_header['offset'] = 0;
2777 $p_header['filename'] = $p_filename;
2778 // TBC : Removed $p_header['stored_filename'] = $v_stored_filename;
2779 $p_header['stored_filename'] = $p_filedescr['stored_filename'];
2780 $p_header['extra'] = '';
2781 $p_header['status'] = 'ok';
2782 $p_header['index'] = -1;
2784 // ----- Look for regular file
2785 if ($p_filedescr['type']=='file') {
2786 $p_header['external'] = 0x00000000;
2787 $p_header['size'] = filesize($p_filename);
2790 // ----- Look for regular folder
2791 else if ($p_filedescr['type']=='folder') {
2792 $p_header['external'] = 0x00000010;
2793 $p_header['mtime'] = filemtime($p_filename);
2794 $p_header['size'] = filesize($p_filename);
2797 // ----- Look for virtual file
2798 else if ($p_filedescr['type'] == 'virtual_file') {
2799 $p_header['external'] = 0x00000000;
2800 $p_header['size'] = strlen($p_filedescr['content']);
2803 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header external extension '".sprintf("0x%X",$p_header['external'])."'");
2805 // ----- Look for filetime
2806 if (isset($p_filedescr['mtime'])) {
2807 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Overload mtime value with :'".$p_filedescr['mtime']."'");
2808 $p_header['mtime'] = $p_filedescr['mtime'];
2810 else if ($p_filedescr['type'] == 'virtual_file') {
2811 $p_header['mtime'] = time();
2812 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Virtual file : use current time '".$p_header['mtime']."' for mtime value.");
2815 $p_header['mtime'] = filemtime($p_filename);
2818 // ------ Look for file comment
2819 if (isset($p_filedescr['comment'])) {
2820 $p_header['comment_len'] = strlen($p_filedescr['comment']);
2821 $p_header['comment'] = $p_filedescr['comment'];
2824 $p_header['comment_len'] = 0;
2825 $p_header['comment'] = '';
2828 // ----- Look for pre-add callback
2829 if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
2830 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_ADD]."()') is defined for the extraction");
2832 // ----- Generate a local information
2833 $v_local_header = array();
2834 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2836 // ----- Call the callback
2837 // Here I do not use call_user_func() because I need to send a reference to the
2839 eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
2840 if ($v_result == 0) {
2841 // ----- Change the file status
2842 $p_header['status'] = "skipped";
2846 // ----- Update the informations
2847 // Only some fields can be modified
2848 if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
2849 $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
2850 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New stored filename is '".$p_header['stored_filename']."'");
2854 // ----- Look for empty stored filename
2855 if ($p_header['stored_filename'] == "") {
2856 $p_header['status'] = "filtered";
2859 // ----- Check the path length
2860 if (strlen($p_header['stored_filename']) > 0xFF) {
2861 $p_header['status'] = 'filename_too_long';
2864 // ----- Look if no error, or file not skipped
2865 if ($p_header['status'] == 'ok') {
2867 // ----- Look for a file
2868 if ($p_filedescr['type'] == 'file') {
2869 // ----- Look for using temporary file to zip
2870 if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
2871 && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
2872 || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
2873 && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])) ) ) {
2874 $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
2875 if ($v_result < PCLZIP_ERR_NO_ERROR) {
2880 // ----- Use "in memory" zip algo
2882 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"In memory compression.");
2883 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Current memory usage : ".memory_get_usage(TRUE)." bytes");
2884 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Current memory peak : ".memory_get_peak_usage(TRUE)." bytes");
2885 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a file");
2887 // ----- Open the source file
2888 if (($v_file = @fopen($p_filename, "rb")) == 0) {
2889 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
2890 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2891 return PclZip::errorCode();
2894 // ----- Read the file content
2895 $v_content = @fread($v_file, $p_header['size']);
2896 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory usage after reading file : ".memory_get_usage(TRUE)." bytes");
2897 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory peak after reading file : ".memory_get_peak_usage(TRUE)." bytes");
2899 // ----- Close the file
2902 // ----- Calculate the CRC
2903 $p_header['crc'] = @crc32($v_content);
2905 // ----- Look for no compression
2906 if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
2907 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be compressed");
2908 // ----- Set header parameters
2909 $p_header['compressed_size'] = $p_header['size'];
2910 $p_header['compression'] = 0;
2913 // ----- Look for normal compression
2915 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will be compressed");
2916 // ----- Compress the content
2917 $v_content = @gzdeflate($v_content);
2918 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory usage after gzdeflate : ".memory_get_usage(TRUE)." bytes");
2919 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory peak after gzdeflate : ".memory_get_peak_usage(TRUE)." bytes");
2921 // ----- Set header parameters
2922 $p_header['compressed_size'] = strlen($v_content);
2923 $p_header['compression'] = 8;
2926 // ----- Call the header generation
2927 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2929 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2933 // ----- Write the compressed (or not) content
2934 @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
2940 // ----- Look for a virtual file (a file from string)
2941 else if ($p_filedescr['type'] == 'virtual_file') {
2943 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Add by string");
2944 $v_content = $p_filedescr['content'];
2946 // ----- Calculate the CRC
2947 $p_header['crc'] = @crc32($v_content);
2949 // ----- Look for no compression
2950 if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
2951 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be compressed");
2952 // ----- Set header parameters
2953 $p_header['compressed_size'] = $p_header['size'];
2954 $p_header['compression'] = 0;
2957 // ----- Look for normal compression
2959 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will be compressed");
2960 // ----- Compress the content
2961 $v_content = @gzdeflate($v_content);
2963 // ----- Set header parameters
2964 $p_header['compressed_size'] = strlen($v_content);
2965 $p_header['compression'] = 8;
2968 // ----- Call the header generation
2969 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2971 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2975 // ----- Write the compressed (or not) content
2976 @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
2979 // ----- Look for a directory
2980 else if ($p_filedescr['type'] == 'folder') {
2981 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a folder");
2982 // ----- Look for directory last '/'
2983 if (@substr($p_header['stored_filename'], -1) != '/') {
2984 $p_header['stored_filename'] .= '/';
2987 // ----- Set the file properties
2988 $p_header['size'] = 0;
2989 //$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
2990 $p_header['external'] = 0x00000010; // Value for a folder : to be checked
2992 // ----- Call the header generation
2993 if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
2995 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3001 // ----- Look for post-add callback
3002 if (isset($p_options[PCLZIP_CB_POST_ADD])) {
3003 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_ADD]."()') is defined for the extraction");
3005 // ----- Generate a local information
3006 $v_local_header = array();
3007 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
3009 // ----- Call the callback
3010 // Here I do not use call_user_func() because I need to send a reference to the
3012 eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
3013 if ($v_result == 0) {
3018 // ----- Update the informations
3019 // Nothing can be modified
3023 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3026 // --------------------------------------------------------------------------------
3028 // --------------------------------------------------------------------------------
3029 // Function : privAddFileUsingTempFile()
3033 // --------------------------------------------------------------------------------
3034 function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
3036 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFileUsingTempFile", "filename='".$p_filedescr['filename']."'");
3037 $v_result=PCLZIP_ERR_NO_ERROR;
3039 // ----- Working variable
3040 $p_filename = $p_filedescr['filename'];
3042 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a file");
3044 // ----- Open the source file
3045 if (($v_file = @fopen($p_filename, "rb")) == 0) {
3046 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
3047 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3048 return PclZip::errorCode();
3051 // ----- Creates a compressed temporary file
3052 $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
3053 if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
3055 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
3056 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3057 return PclZip::errorCode();
3060 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3061 $v_size = filesize($p_filename);
3062 while ($v_size != 0) {
3063 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3064 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read ".$v_read_size." bytes");
3065 $v_buffer = @fread($v_file, $v_read_size);
3066 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
3067 @gzputs($v_file_compressed, $v_buffer, $v_read_size);
3068 $v_size -= $v_read_size;
3071 // ----- Close the file
3073 @gzclose($v_file_compressed);
3075 // ----- Check the minimum file size
3076 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "gzip file size ".filesize($v_gzip_temp_name));
3077 if (filesize($v_gzip_temp_name) < 18) {
3078 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
3079 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3080 return PclZip::errorCode();
3083 // ----- Extract the compressed attributes
3084 if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
3085 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
3086 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3087 return PclZip::errorCode();
3090 // ----- Read the gzip file header
3091 $v_binary_data = @fread($v_file_compressed, 10);
3092 $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
3094 // ----- Check some parameters
3095 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[id1]='.bin2hex($v_data_header['id1']));
3096 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[id2]='.bin2hex($v_data_header['id2']));
3097 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[cm]='.bin2hex($v_data_header['cm']));
3098 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[flag]='.bin2hex($v_data_header['flag']));
3099 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[mtime]='.$v_data_header['mtime']);
3100 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[xfl]='.bin2hex($v_data_header['xfl']));
3101 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[os]='.bin2hex($v_data_header['os']));
3102 $v_data_header['os'] = bin2hex($v_data_header['os']);
3104 // ----- Read the gzip file footer
3105 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position after header ".ftell($v_file_compressed));
3106 @fseek($v_file_compressed, filesize($v_gzip_temp_name)-8);
3107 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position at beginning of footer ".ftell($v_file_compressed));
3108 $v_binary_data = @fread($v_file_compressed, 8);
3109 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position after footer ".ftell($v_file_compressed));
3110 $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
3112 // ----- Set the attributes
3113 $p_header['compression'] = ord($v_data_header['cm']);
3114 //$p_header['mtime'] = $v_data_header['mtime'];
3115 $p_header['crc'] = $v_data_footer['crc'];
3116 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Compressed size ".(filesize($v_gzip_temp_name)-18));
3117 $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
3119 // ----- Close the file
3120 @fclose($v_file_compressed);
3122 // ----- Call the header generation
3123 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
3124 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3128 // ----- Add the compressed data
3129 if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0)
3131 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
3132 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3133 return PclZip::errorCode();
3136 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3137 fseek($v_file_compressed, 10);
3138 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position before reading compressed data ".ftell($v_file_compressed));
3139 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, ' '.$p_header['compressed_size'].' bytes to read');
3140 $v_size = $p_header['compressed_size'];
3141 while ($v_size != 0)
3143 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3144 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read ".$v_read_size." bytes");
3145 $v_buffer = @fread($v_file_compressed, $v_read_size);
3146 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
3147 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
3148 $v_size -= $v_read_size;
3151 // ----- Close the file
3152 @fclose($v_file_compressed);
3154 // ----- Unlink the temporary file
3155 @unlink($v_gzip_temp_name);
3158 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3161 // --------------------------------------------------------------------------------
3163 // --------------------------------------------------------------------------------
3164 // Function : privCalculateStoredFilename()
3166 // Based on file descriptor properties and global options, this method
3167 // calculate the filename that will be stored in the archive.
3170 // --------------------------------------------------------------------------------
3171 function privCalculateStoredFilename(&$p_filedescr, &$p_options)
3173 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCalculateStoredFilename", "filename='".$p_filedescr['filename']."'");
3176 // ----- Working variables
3177 $p_filename = $p_filedescr['filename'];
3178 if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
3179 $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
3184 if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
3185 $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
3190 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Remove path ='".$p_remove_dir."'");
3191 if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
3192 $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
3195 $p_remove_all_dir = 0;
3198 // ----- Look for full name change
3199 if (isset($p_filedescr['new_full_name'])) {
3200 // ----- Remove drive letter if any
3201 $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
3202 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing full name of '".$p_filename."' for '".$v_stored_filename."'");
3205 // ----- Look for path and/or short name change
3208 // ----- Look for short name change
3209 // Its when we cahnge just the filename but not the path
3210 if (isset($p_filedescr['new_short_name'])) {
3211 $v_path_info = pathinfo($p_filename);
3213 if ($v_path_info['dirname'] != '') {
3214 $v_dir = $v_path_info['dirname'].'/';
3216 $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
3217 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing short name of '".$p_filename."' for '".$v_stored_filename."'");
3220 // ----- Calculate the stored filename
3221 $v_stored_filename = $p_filename;
3224 // ----- Look for all path to remove
3225 if ($p_remove_all_dir) {
3226 $v_stored_filename = basename($p_filename);
3227 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove all path selected change '".$p_filename."' for '".$v_stored_filename."'");
3229 // ----- Look for partial path remove
3230 else if ($p_remove_dir != "") {
3231 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Partial path to remove");
3232 if (substr($p_remove_dir, -1) != '/')
3233 $p_remove_dir .= "/";
3235 if ( (substr($p_filename, 0, 2) == "./")
3236 || (substr($p_remove_dir, 0, 2) == "./")) {
3238 if ( (substr($p_filename, 0, 2) == "./")
3239 && (substr($p_remove_dir, 0, 2) != "./")) {
3240 $p_remove_dir = "./".$p_remove_dir;
3242 if ( (substr($p_filename, 0, 2) != "./")
3243 && (substr($p_remove_dir, 0, 2) == "./")) {
3244 $p_remove_dir = substr($p_remove_dir, 2);
3248 $v_compare = PclZipUtilPathInclusion($p_remove_dir,
3249 $v_stored_filename);
3250 if ($v_compare > 0) {
3251 if ($v_compare == 2) {
3252 $v_stored_filename = "";
3253 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Path to remove is the current folder");
3256 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove path '$p_remove_dir' in file '$v_stored_filename'");
3257 $v_stored_filename = substr($v_stored_filename,
3258 strlen($p_remove_dir));
3259 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Result is '$v_stored_filename'");
3264 // ----- Remove drive letter if any
3265 $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
3267 // ----- Look for path to add
3268 if ($p_add_dir != "") {
3269 if (substr($p_add_dir, -1) == "/")
3270 $v_stored_filename = $p_add_dir.$v_stored_filename;
3272 $v_stored_filename = $p_add_dir."/".$v_stored_filename;
3273 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
3277 // ----- Filename (reduce the path of stored name)
3278 $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
3279 $p_filedescr['stored_filename'] = $v_stored_filename;
3280 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Stored filename will be '".$p_filedescr['stored_filename']."', strlen ".strlen($p_filedescr['stored_filename']));
3283 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3286 // --------------------------------------------------------------------------------
3288 // --------------------------------------------------------------------------------
3289 // Function : privWriteFileHeader()
3293 // --------------------------------------------------------------------------------
3294 function privWriteFileHeader(&$p_header)
3296 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
3299 // ----- Store the offset position of the file
3300 $p_header['offset'] = ftell($this->zip_fd);
3301 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'File offset of the header :'.$p_header['offset']);
3303 // ----- Transform UNIX mtime to DOS format mdate/mtime
3304 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
3305 $v_date = getdate($p_header['mtime']);
3306 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
3307 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
3309 // ----- Packed data
3310 $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
3311 $p_header['version_extracted'], $p_header['flag'],
3312 $p_header['compression'], $v_mtime, $v_mdate,
3313 $p_header['crc'], $p_header['compressed_size'],
3315 strlen($p_header['stored_filename']),
3316 $p_header['extra_len']);
3318 // ----- Write the first 148 bytes of the header in the archive
3319 fputs($this->zip_fd, $v_binary_data, 30);
3321 // ----- Write the variable fields
3322 if (strlen($p_header['stored_filename']) != 0)
3324 fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
3326 if ($p_header['extra_len'] != 0)
3328 fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
3332 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3335 // --------------------------------------------------------------------------------
3337 // --------------------------------------------------------------------------------
3338 // Function : privWriteCentralFileHeader()
3342 // --------------------------------------------------------------------------------
3343 function privWriteCentralFileHeader(&$p_header)
3345 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
3349 //for(reset($p_header); $key = key($p_header); next($p_header)) {
3350 // //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "header[$key] = ".$p_header[$key]);
3353 // ----- Transform UNIX mtime to DOS format mdate/mtime
3354 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
3355 $v_date = getdate($p_header['mtime']);
3356 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
3357 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
3359 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment size : \''.$p_header['comment_len'].'\'');
3361 // ----- Packed data
3362 $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
3363 $p_header['version'], $p_header['version_extracted'],
3364 $p_header['flag'], $p_header['compression'],
3365 $v_mtime, $v_mdate, $p_header['crc'],
3366 $p_header['compressed_size'], $p_header['size'],
3367 strlen($p_header['stored_filename']),
3368 $p_header['extra_len'], $p_header['comment_len'],
3369 $p_header['disk'], $p_header['internal'],
3370 $p_header['external'], $p_header['offset']);
3372 // ----- Write the 42 bytes of the header in the zip file
3373 fputs($this->zip_fd, $v_binary_data, 46);
3375 // ----- Write the variable fields
3376 if (strlen($p_header['stored_filename']) != 0)
3378 fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
3380 if ($p_header['extra_len'] != 0)
3382 fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
3384 if ($p_header['comment_len'] != 0)
3386 fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
3390 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3393 // --------------------------------------------------------------------------------
3395 // --------------------------------------------------------------------------------
3396 // Function : privWriteCentralHeader()
3400 // --------------------------------------------------------------------------------
3401 function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
3403 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralHeader", 'nb_entries='.$p_nb_entries.', size='.$p_size.', offset='.$p_offset.', comment="'.$p_comment.'"');
3406 // ----- Packed data
3407 $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
3408 $p_nb_entries, $p_size,
3409 $p_offset, strlen($p_comment));
3411 // ----- Write the 22 bytes of the header in the zip file
3412 fputs($this->zip_fd, $v_binary_data, 22);
3414 // ----- Write the variable fields
3415 if (strlen($p_comment) != 0)
3417 fputs($this->zip_fd, $p_comment, strlen($p_comment));
3421 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3424 // --------------------------------------------------------------------------------
3426 // --------------------------------------------------------------------------------
3427 // Function : privList()
3431 // --------------------------------------------------------------------------------
3432 function privList(&$p_list)
3434 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privList", "list");
3437 // ----- Magic quotes trick
3438 $this->privDisableMagicQuotes();
3440 // ----- Open the zip file
3441 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
3442 if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
3444 // ----- Magic quotes trick
3445 $this->privSwapBackMagicQuotes();
3448 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
3451 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3452 return PclZip::errorCode();
3455 // ----- Read the central directory informations
3456 $v_central_dir = array();
3457 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3459 $this->privSwapBackMagicQuotes();
3460 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3464 // ----- Go to beginning of Central Dir
3465 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Offset : ".$v_central_dir['offset']."'");
3466 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
3467 @rewind($this->zip_fd);
3468 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
3469 if (@fseek($this->zip_fd, $v_central_dir['offset']))
3471 $this->privSwapBackMagicQuotes();
3474 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3477 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3478 return PclZip::errorCode();
3480 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
3482 // ----- Read each entry
3483 for ($i=0; $i<$v_central_dir['entries']; $i++)
3485 // ----- Read the file header
3486 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3488 $this->privSwapBackMagicQuotes();
3489 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3492 $v_header['index'] = $i;
3494 // ----- Get the only interesting attributes
3495 $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
3499 // ----- Close the zip file
3500 $this->privCloseFd();
3502 // ----- Magic quotes trick
3503 $this->privSwapBackMagicQuotes();
3506 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3509 // --------------------------------------------------------------------------------
3511 // --------------------------------------------------------------------------------
3512 // Function : privConvertHeader2FileInfo()
3514 // This function takes the file informations from the central directory
3515 // entries and extract the interesting parameters that will be given back.
3516 // The resulting file infos are set in the array $p_info
3517 // $p_info['filename'] : Filename with full path. Given by user (add),
3518 // extracted in the filesystem (extract).
3519 // $p_info['stored_filename'] : Stored filename in the archive.
3520 // $p_info['size'] = Size of the file.
3521 // $p_info['compressed_size'] = Compressed size of the file.
3522 // $p_info['mtime'] = Last modification date of the file.
3523 // $p_info['comment'] = Comment associated with the file.
3524 // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
3525 // $p_info['status'] = status of the action on the file.
3526 // $p_info['crc'] = CRC of the file content.
3529 // --------------------------------------------------------------------------------
3530 function privConvertHeader2FileInfo($p_header, &$p_info)
3532 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privConvertHeader2FileInfo", "Filename='".$p_header['filename']."'");
3535 // ----- Get the interesting attributes
3536 $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
3537 $p_info['filename'] = $v_temp_path;
3538 $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
3539 $p_info['stored_filename'] = $v_temp_path;
3540 $p_info['size'] = $p_header['size'];
3541 $p_info['compressed_size'] = $p_header['compressed_size'];
3542 $p_info['mtime'] = $p_header['mtime'];
3543 $p_info['comment'] = $p_header['comment'];
3544 $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
3545 $p_info['index'] = $p_header['index'];
3546 $p_info['status'] = $p_header['status'];
3547 $p_info['crc'] = $p_header['crc'];
3550 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3553 // --------------------------------------------------------------------------------
3555 // --------------------------------------------------------------------------------
3556 // Function : privExtractByRule()
3558 // Extract a file or directory depending of rules (by index, by name, ...)
3560 // $p_file_list : An array where will be placed the properties of each
3562 // $p_path : Path to add while writing the extracted files
3563 // $p_remove_path : Path to remove (from the file memorized path) while writing the
3564 // extracted files. If the path does not match the file path,
3565 // the file is extracted with its memorized path.
3566 // $p_remove_path does not apply to 'list' mode.
3567 // $p_path and $p_remove_path are commulative.
3569 // 1 on success,0 or less on error (see error code list)
3570 // --------------------------------------------------------------------------------
3571 function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3573 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privExtractByRule", "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
3576 // ----- Magic quotes trick
3577 $this->privDisableMagicQuotes();
3579 // ----- Check the path
3580 if ( ($p_path == "")
3581 || ( (substr($p_path, 0, 1) != "/")
3582 && (substr($p_path, 0, 3) != "../")
3583 && (substr($p_path,1,2)!=":/")))
3584 $p_path = "./".$p_path;
3586 // ----- Reduce the path last (and duplicated) '/'
3587 if (($p_path != "./") && ($p_path != "/"))
3589 // ----- Look for the path end '/'
3590 while (substr($p_path, -1) == "/")
3592 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
3593 $p_path = substr($p_path, 0, strlen($p_path)-1);
3594 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
3598 // ----- Look for path to remove format (should end by /)
3599 if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
3601 $p_remove_path .= '/';
3603 $p_remove_path_size = strlen($p_remove_path);
3605 // ----- Open the zip file
3606 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
3607 if (($v_result = $this->privOpenFd('rb')) != 1)
3609 $this->privSwapBackMagicQuotes();
3610 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3614 // ----- Read the central directory informations
3615 $v_central_dir = array();
3616 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3618 // ----- Close the zip file
3619 $this->privCloseFd();
3620 $this->privSwapBackMagicQuotes();
3622 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3626 // ----- Start at beginning of Central Dir
3627 $v_pos_entry = $v_central_dir['offset'];
3629 // ----- Read each entry
3631 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
3633 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry : '$i'");
3635 // ----- Read next Central dir entry
3636 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position before rewind : ".ftell($this->zip_fd)."'");
3637 @rewind($this->zip_fd);
3638 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position after rewind : ".ftell($this->zip_fd)."'");
3639 if (@fseek($this->zip_fd, $v_pos_entry))
3641 // ----- Close the zip file
3642 $this->privCloseFd();
3643 $this->privSwapBackMagicQuotes();
3646 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3649 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3650 return PclZip::errorCode();
3652 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position after fseek : ".ftell($this->zip_fd)."'");
3654 // ----- Read the file header
3655 $v_header = array();
3656 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3658 // ----- Close the zip file
3659 $this->privCloseFd();
3660 $this->privSwapBackMagicQuotes();
3662 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3666 // ----- Store the index
3667 $v_header['index'] = $i;
3669 // ----- Store the file position
3670 $v_pos_entry = ftell($this->zip_fd);
3672 // ----- Look for the specific extract rules
3675 // ----- Look for extract by name rule
3676 if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
3677 && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
3678 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
3680 // ----- Look if the filename is in the list
3681 for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
3682 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
3684 // ----- Look for a directory
3685 if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
3686 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
3688 // ----- Look if the directory is in the filename path
3689 if ( (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
3690 && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
3691 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
3695 // ----- Look for a filename
3696 elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
3697 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
3703 // ----- Look for extract by ereg rule
3704 else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
3705 && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
3706 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
3708 if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
3709 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
3714 // ----- Look for extract by preg rule
3715 else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
3716 && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
3717 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
3719 if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
3720 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
3725 // ----- Look for extract by index rule
3726 else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
3727 && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
3728 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
3730 // ----- Look if the index is in the list
3731 for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
3732 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look if index '$i' is in [".$p_options[PCLZIP_OPT_BY_INDEX][$j]['start'].",".$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']."]");
3734 if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
3735 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
3738 if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
3739 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
3743 if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
3744 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
3750 // ----- Look for no rule, which means extract all the archive
3752 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with no rule (extract all)");
3756 // ----- Check compression method
3758 && ( ($v_header['compression'] != 8)
3759 && ($v_header['compression'] != 0))) {
3760 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported compression method (".$v_header['compression'].")");
3761 $v_header['status'] = 'unsupported_compression';
3763 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3764 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3765 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3766 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3768 $this->privSwapBackMagicQuotes();
3770 PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
3771 "Filename '".$v_header['stored_filename']."' is "
3772 ."compressed by an unsupported compression "
3773 ."method (".$v_header['compression'].") ");
3775 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3776 return PclZip::errorCode();
3780 // ----- Check encrypted files
3781 if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
3782 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported file encryption");
3783 $v_header['status'] = 'unsupported_encryption';
3785 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3786 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3787 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3788 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
3790 $this->privSwapBackMagicQuotes();
3792 PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
3793 "Unsupported encryption for "
3794 ." filename '".$v_header['stored_filename']
3797 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3798 return PclZip::errorCode();
3802 // ----- Look for real extraction
3803 if (($v_extract) && ($v_header['status'] != 'ok')) {
3804 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "No need for extract");
3805 $v_result = $this->privConvertHeader2FileInfo($v_header,
3806 $p_file_list[$v_nb_extracted++]);
3807 if ($v_result != 1) {
3808 $this->privCloseFd();
3809 $this->privSwapBackMagicQuotes();
3810 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3817 // ----- Look for real extraction
3820 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file '".$v_header['filename']."', index '$i'");
3822 // ----- Go to the file position
3823 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
3824 @rewind($this->zip_fd);
3825 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
3826 if (@fseek($this->zip_fd, $v_header['offset']))
3828 // ----- Close the zip file
3829 $this->privCloseFd();
3831 $this->privSwapBackMagicQuotes();
3834 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3837 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3838 return PclZip::errorCode();
3840 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
3842 // ----- Look for extraction as string
3843 if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
3845 // ----- Extracting the file
3846 $v_result1 = $this->privExtractFileAsString($v_header, $v_string);
3847 if ($v_result1 < 1) {
3848 $this->privCloseFd();
3849 $this->privSwapBackMagicQuotes();
3850 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
3854 // ----- Get the only interesting attributes
3855 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
3857 // ----- Close the zip file
3858 $this->privCloseFd();
3859 $this->privSwapBackMagicQuotes();
3861 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3865 // ----- Set the file content
3866 $p_file_list[$v_nb_extracted]['content'] = $v_string;
3868 // ----- Next extracted file
3871 // ----- Look for user callback abort
3872 if ($v_result1 == 2) {
3876 // ----- Look for extraction in standard output
3877 elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
3878 && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
3879 // ----- Extracting the file in standard output
3880 $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
3881 if ($v_result1 < 1) {
3882 $this->privCloseFd();
3883 $this->privSwapBackMagicQuotes();
3884 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
3888 // ----- Get the only interesting attributes
3889 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
3890 $this->privCloseFd();
3891 $this->privSwapBackMagicQuotes();
3892 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3896 // ----- Look for user callback abort
3897 if ($v_result1 == 2) {
3901 // ----- Look for normal extraction
3903 // ----- Extracting the file
3904 $v_result1 = $this->privExtractFile($v_header,
3905 $p_path, $p_remove_path,
3908 if ($v_result1 < 1) {
3909 $this->privCloseFd();
3910 $this->privSwapBackMagicQuotes();
3911 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
3915 // ----- Get the only interesting attributes
3916 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
3918 // ----- Close the zip file
3919 $this->privCloseFd();
3920 $this->privSwapBackMagicQuotes();
3922 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3926 // ----- Look for user callback abort
3927 if ($v_result1 == 2) {
3934 // ----- Close the zip file
3935 $this->privCloseFd();
3936 $this->privSwapBackMagicQuotes();
3939 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3942 // --------------------------------------------------------------------------------
3944 // --------------------------------------------------------------------------------
3945 // Function : privExtractFile()
3951 // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
3952 // --------------------------------------------------------------------------------
3953 function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3955 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFile', "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
3958 // ----- Read the file header
3959 if (($v_result = $this->privReadFileHeader($v_header)) != 1)
3962 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3966 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
3968 // ----- Check that the file header is coherent with $p_entry info
3969 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
3973 // ----- Look for all path to remove
3974 if ($p_remove_all_path == true) {
3975 // ----- Look for folder entry that not need to be extracted
3976 if (($p_entry['external']&0x00000010)==0x00000010) {
3977 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The entry is a folder : need to be filtered");
3979 $p_entry['status'] = "filtered";
3981 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3985 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "All path is removed");
3986 // ----- Get the basename of the path
3987 $p_entry['filename'] = basename($p_entry['filename']);
3990 // ----- Look for path to remove
3991 else if ($p_remove_path != "")
3993 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look for some path to remove");
3994 if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
3996 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The folder is the same as the removed path '".$p_entry['filename']."'");
3998 // ----- Change the file status
3999 $p_entry['status'] = "filtered";
4002 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4006 $p_remove_path_size = strlen($p_remove_path);
4007 if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
4009 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file '".$p_entry['filename']."'");
4011 // ----- Remove the path
4012 $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
4014 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Resulting file is '".$p_entry['filename']."'");
4018 // ----- Add the path
4019 if ($p_path != '') {
4020 $p_entry['filename'] = $p_path."/".$p_entry['filename'];
4023 // ----- Check a base_dir_restriction
4024 if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
4025 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Check the extract directory restriction");
4027 = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
4028 $p_entry['filename']);
4029 if ($v_inclusion == 0) {
4030 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_EXTRACT_DIR_RESTRICTION is selected, file is outside restriction");
4032 PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
4033 "Filename '".$p_entry['filename']."' is "
4034 ."outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
4036 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4037 return PclZip::errorCode();
4041 // ----- Look for pre-extract callback
4042 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
4043 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
4045 // ----- Generate a local information
4046 $v_local_header = array();
4047 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4049 // ----- Call the callback
4050 // Here I do not use call_user_func() because I need to send a reference to the
4052 eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
4053 if ($v_result == 0) {
4054 // ----- Change the file status
4055 $p_entry['status'] = "skipped";
4059 // ----- Look for abort result
4060 if ($v_result == 2) {
4061 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
4062 // ----- This status is internal and will be changed in 'skipped'
4063 $p_entry['status'] = "aborted";
4064 $v_result = PCLZIP_ERR_USER_ABORTED;
4067 // ----- Update the informations
4068 // Only some fields can be modified
4069 $p_entry['filename'] = $v_local_header['filename'];
4070 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
4073 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
4075 // ----- Look if extraction should be done
4076 if ($p_entry['status'] == 'ok') {
4078 // ----- Look for specific actions while the file exist
4079 if (file_exists($p_entry['filename']))
4081 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$p_entry['filename']."' already exists");
4083 // ----- Look if file is a directory
4084 if (is_dir($p_entry['filename']))
4086 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is a directory");
4088 // ----- Change the file status
4089 $p_entry['status'] = "already_a_directory";
4091 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
4092 // For historical reason first PclZip implementation does not stop
4093 // when this kind of error occurs.
4094 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
4095 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
4096 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
4098 PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
4099 "Filename '".$p_entry['filename']."' is "
4100 ."already used by an existing directory");
4102 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4103 return PclZip::errorCode();
4106 // ----- Look if file is write protected
4107 else if (!is_writeable($p_entry['filename']))
4109 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is write protected");
4111 // ----- Change the file status
4112 $p_entry['status'] = "write_protected";
4114 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
4115 // For historical reason first PclZip implementation does not stop
4116 // when this kind of error occurs.
4117 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
4118 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
4119 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
4121 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
4122 "Filename '".$p_entry['filename']."' exists "
4123 ."and is write protected");
4125 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4126 return PclZip::errorCode();
4130 // ----- Look if the extracted file is older
4131 else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
4133 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is newer (".date("l dS of F Y h:i:s A", filemtime($p_entry['filename'])).") than the extracted file (".date("l dS of F Y h:i:s A", $p_entry['mtime']).")");
4134 // ----- Change the file status
4135 if ( (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
4136 && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
4137 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_REPLACE_NEWER is selected, file will be replaced");
4140 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be replaced");
4141 $p_entry['status'] = "newer_exist";
4143 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
4144 // For historical reason first PclZip implementation does not stop
4145 // when this kind of error occurs.
4146 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
4147 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
4148 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
4150 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
4151 "Newer version of '".$p_entry['filename']."' exists "
4152 ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
4154 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4155 return PclZip::errorCode();
4160 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is older than the extrated one - will be replaced by the extracted one (".date("l dS of F Y h:i:s A", filemtime($p_entry['filename'])).") than the extracted file (".date("l dS of F Y h:i:s A", $p_entry['mtime']).")");
4164 // ----- Check the directory availability and create it if necessary
4166 if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
4167 $v_dir_to_check = $p_entry['filename'];
4168 else if (!strstr($p_entry['filename'], "/"))
4169 $v_dir_to_check = "";
4171 $v_dir_to_check = dirname($p_entry['filename']);
4173 if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
4174 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '".$p_entry['filename']."'");
4176 // ----- Change the file status
4177 $p_entry['status'] = "path_creation_fail";
4180 ////--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4187 // ----- Look if extraction should be done
4188 if ($p_entry['status'] == 'ok') {
4190 // ----- Do the extraction (if not a folder)
4191 if (!(($p_entry['external']&0x00000010)==0x00000010))
4193 // ----- Look for not compressed file
4194 if ($p_entry['compression'] == 0) {
4195 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
4197 // ----- Opening destination file
4198 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
4200 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
4202 // ----- Change the file status
4203 $p_entry['status'] = "write_error";
4206 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4210 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read '".$p_entry['size']."' bytes");
4212 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
4213 $v_size = $p_entry['compressed_size'];
4214 while ($v_size != 0)
4216 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4217 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read $v_read_size bytes");
4218 $v_buffer = @fread($this->zip_fd, $v_read_size);
4219 /* Try to speed up the code
4220 $v_binary_data = pack('a'.$v_read_size, $v_buffer);
4221 @fwrite($v_dest_file, $v_binary_data, $v_read_size);
4223 @fwrite($v_dest_file, $v_buffer, $v_read_size);
4224 $v_size -= $v_read_size;
4227 // ----- Closing the destination file
4228 fclose($v_dest_file);
4230 // ----- Change the file mtime
4231 touch($p_entry['filename'], $p_entry['mtime']);
4236 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file (Compression method ".$p_entry['compression'].")");
4238 // Need to be finished
4239 if (($p_entry['flag'] & 1) == 1) {
4240 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File is encrypted");
4241 PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \''.$p_entry['filename'].'\' is encrypted. Encrypted files are not supported.');
4242 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4243 return PclZip::errorCode();
4247 // ----- Look for using temporary file to unzip
4248 if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
4249 && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
4250 || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
4251 && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])) ) ) {
4252 $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
4253 if ($v_result < PCLZIP_ERR_NO_ERROR) {
4258 // ----- Look for extract in memory
4261 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read '".$p_entry['compressed_size']."' compressed bytes");
4263 // ----- Read the compressed file in a buffer (one shot)
4264 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
4266 // ----- Decompress the file
4267 $v_file_content = @gzinflate($v_buffer);
4269 if ($v_file_content === FALSE) {
4270 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to inflate compressed file");
4272 // ----- Change the file status
4274 $p_entry['status'] = "error";
4276 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4280 // ----- Opening destination file
4281 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
4282 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
4284 // ----- Change the file status
4285 $p_entry['status'] = "write_error";
4287 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4291 // ----- Write the uncompressed data
4292 @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
4293 unset($v_file_content);
4295 // ----- Closing the destination file
4296 @fclose($v_dest_file);
4300 // ----- Change the file mtime
4301 @touch($p_entry['filename'], $p_entry['mtime']);
4304 // ----- Look for chmod option
4305 if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
4306 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "chmod option activated '".$p_options[PCLZIP_OPT_SET_CHMOD]."'");
4308 // ----- Change the mode of the file
4309 @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
4312 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
4316 // ----- Change abort status
4317 if ($p_entry['status'] == "aborted") {
4318 $p_entry['status'] = "skipped";
4321 // ----- Look for post-extract callback
4322 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
4323 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
4325 // ----- Generate a local information
4326 $v_local_header = array();
4327 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4329 // ----- Call the callback
4330 // Here I do not use call_user_func() because I need to send a reference to the
4332 eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
4334 // ----- Look for abort result
4335 if ($v_result == 2) {
4336 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
4337 $v_result = PCLZIP_ERR_USER_ABORTED;
4342 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4345 // --------------------------------------------------------------------------------
4347 // --------------------------------------------------------------------------------
4348 // Function : privExtractFileUsingTempFile()
4352 // --------------------------------------------------------------------------------
4353 function privExtractFileUsingTempFile(&$p_entry, &$p_options)
4355 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileUsingTempFile', "filename='".$p_entry['filename']."'");
4358 // ----- Creates a temporary file
4359 $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
4360 if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
4362 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
4363 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4364 return PclZip::errorCode();
4367 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Start extraction of '".$p_entry['filename']."'");
4369 // ----- Write gz file format header
4370 $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
4371 @fwrite($v_dest_file, $v_binary_data, 10);
4373 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
4374 $v_size = $p_entry['compressed_size'];
4375 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Compressed Size :".$v_size."");
4376 while ($v_size != 0)
4378 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4379 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read ".$v_read_size." bytes");
4380 $v_buffer = @fread($this->zip_fd, $v_read_size);
4381 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
4382 @fwrite($v_dest_file, $v_buffer, $v_read_size);
4383 $v_size -= $v_read_size;
4386 // ----- Write gz file format footer
4387 $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
4388 @fwrite($v_dest_file, $v_binary_data, 8);
4390 // ----- Close the temporary file
4391 @fclose($v_dest_file);
4393 // ----- Opening destination file
4394 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
4395 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
4396 $p_entry['status'] = "write_error";
4397 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4401 // ----- Open the temporary gz file
4402 if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
4403 @fclose($v_dest_file);
4404 $p_entry['status'] = "read_error";
4405 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
4406 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4407 return PclZip::errorCode();
4410 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'File size is '.filesize($v_gzip_temp_name));
4411 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
4413 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
4414 $v_size = $p_entry['size'];
4415 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size :".$v_size."");
4416 while ($v_size != 0) {
4417 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4418 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read ".$v_read_size." bytes");
4419 $v_buffer = @gzread($v_src_file, $v_read_size);
4420 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
4421 @fwrite($v_dest_file, $v_buffer, $v_read_size);
4422 $v_size -= $v_read_size;
4424 @fclose($v_dest_file);
4425 @gzclose($v_src_file);
4427 // ----- Delete the temporary file
4428 @unlink($v_gzip_temp_name);
4431 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4434 // --------------------------------------------------------------------------------
4436 // --------------------------------------------------------------------------------
4437 // Function : privExtractFileInOutput()
4441 // --------------------------------------------------------------------------------
4442 function privExtractFileInOutput(&$p_entry, &$p_options)
4444 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileInOutput', "");
4447 // ----- Read the file header
4448 if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
4449 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4453 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
4455 // ----- Check that the file header is coherent with $p_entry info
4456 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
4460 // ----- Look for pre-extract callback
4461 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
4462 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
4464 // ----- Generate a local information
4465 $v_local_header = array();
4466 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4468 // ----- Call the callback
4469 // Here I do not use call_user_func() because I need to send a reference to the
4471 eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
4472 if ($v_result == 0) {
4473 // ----- Change the file status
4474 $p_entry['status'] = "skipped";
4478 // ----- Look for abort result
4479 if ($v_result == 2) {
4480 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
4481 // ----- This status is internal and will be changed in 'skipped'
4482 $p_entry['status'] = "aborted";
4483 $v_result = PCLZIP_ERR_USER_ABORTED;
4486 // ----- Update the informations
4487 // Only some fields can be modified
4488 $p_entry['filename'] = $v_local_header['filename'];
4489 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
4493 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
4495 // ----- Look if extraction should be done
4496 if ($p_entry['status'] == 'ok') {
4498 // ----- Do the extraction (if not a folder)
4499 if (!(($p_entry['external']&0x00000010)==0x00000010)) {
4500 // ----- Look for not compressed file
4501 if ($p_entry['compressed_size'] == $p_entry['size']) {
4502 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
4503 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
4505 // ----- Read the file in a buffer (one shot)
4506 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
4508 // ----- Send the file to the output
4513 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file");
4514 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Reading '".$p_entry['size']."' bytes");
4516 // ----- Read the compressed file in a buffer (one shot)
4517 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
4519 // ----- Decompress the file
4520 $v_file_content = gzinflate($v_buffer);
4523 // ----- Send the file to the output
4524 echo $v_file_content;
4525 unset($v_file_content);
4527 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
4531 // ----- Change abort status
4532 if ($p_entry['status'] == "aborted") {
4533 $p_entry['status'] = "skipped";
4536 // ----- Look for post-extract callback
4537 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
4538 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
4540 // ----- Generate a local information
4541 $v_local_header = array();
4542 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4544 // ----- Call the callback
4545 // Here I do not use call_user_func() because I need to send a reference to the
4547 eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
4549 // ----- Look for abort result
4550 if ($v_result == 2) {
4551 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
4552 $v_result = PCLZIP_ERR_USER_ABORTED;
4556 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4559 // --------------------------------------------------------------------------------
4561 // --------------------------------------------------------------------------------
4562 // Function : privExtractFileAsString()
4566 // --------------------------------------------------------------------------------
4567 function privExtractFileAsString(&$p_entry, &$p_string)
4569 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileAsString', "p_entry['filename']='".$p_entry['filename']."'");
4572 // ----- Read the file header
4573 $v_header = array();
4574 if (($v_result = $this->privReadFileHeader($v_header)) != 1)
4577 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4581 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
4583 // ----- Check that the file header is coherent with $p_entry info
4584 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
4588 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file in string (with path) '".$p_entry['filename']."', size '$v_header[size]'");
4590 // ----- Do the extraction (if not a folder)
4591 if (!(($p_entry['external']&0x00000010)==0x00000010))
4593 // ----- Look for not compressed file
4594 // if ($p_entry['compressed_size'] == $p_entry['size'])
4595 if ($p_entry['compression'] == 0) {
4596 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
4597 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
4599 // ----- Reading the file
4600 $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
4603 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file (compression method '".$p_entry['compression']."')");
4605 // ----- Reading the file
4606 $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
4608 // ----- Decompress the file
4609 if (($p_string = @gzinflate($v_data)) === FALSE) {
4615 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
4618 // TBC : error : can not extract a folder in a string
4622 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4625 // --------------------------------------------------------------------------------
4627 // --------------------------------------------------------------------------------
4628 // Function : privReadFileHeader()
4632 // --------------------------------------------------------------------------------
4633 function privReadFileHeader(&$p_header)
4635 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadFileHeader", "");
4638 // ----- Read the 4 bytes signature
4639 $v_binary_data = @fread($this->zip_fd, 4);
4640 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
4641 $v_data = unpack('Vid', $v_binary_data);
4642 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
4644 // ----- Check signature
4645 if ($v_data['id'] != 0x04034b50)
4647 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid File header");
4650 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
4653 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4654 return PclZip::errorCode();
4657 // ----- Read the first 42 bytes of the header
4658 $v_binary_data = fread($this->zip_fd, 26);
4660 // ----- Look for invalid block size
4661 if (strlen($v_binary_data) != 26)
4663 $p_header['filename'] = "";
4664 $p_header['status'] = "invalid_header";
4665 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
4668 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
4671 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4672 return PclZip::errorCode();
4675 // ----- Extract the values
4676 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header : '".$v_binary_data."'");
4677 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header (Hex) : '".bin2hex($v_binary_data)."'");
4678 $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
4680 // ----- Get filename
4681 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "File name length : ".$v_data['filename_len']);
4682 $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
4683 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename : \''.$p_header['filename'].'\'');
4685 // ----- Get extra_fields
4686 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extra field length : ".$v_data['extra_len']);
4687 if ($v_data['extra_len'] != 0) {
4688 $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
4691 $p_header['extra'] = '';
4693 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Extra field : \''.bin2hex($p_header['extra']).'\'');
4695 // ----- Extract properties
4696 $p_header['version_extracted'] = $v_data['version'];
4697 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : ('.$p_header['version_extracted'].') \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
4698 $p_header['compression'] = $v_data['compression'];
4699 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compression method : \''.$p_header['compression'].'\'');
4700 $p_header['size'] = $v_data['size'];
4701 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_header['size'].'\'');
4702 $p_header['compressed_size'] = $v_data['compressed_size'];
4703 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
4704 $p_header['crc'] = $v_data['crc'];
4705 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
4706 $p_header['flag'] = $v_data['flag'];
4707 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag : \''.$p_header['flag'].'\'');
4708 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag bit 11 (from right) : \''.($p_header['flag']&0x0400).'\'');
4709 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag bit 11 (from left) : \''.($p_header['flag']&0x0020).'\'');
4710 $p_header['filename_len'] = $v_data['filename_len'];
4711 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename_len : \''.$p_header['filename_len'].'\'');
4713 // ----- Recuperate date in UNIX format
4714 $p_header['mdate'] = $v_data['mdate'];
4715 $p_header['mtime'] = $v_data['mtime'];
4716 if ($p_header['mdate'] && $p_header['mtime'])
4718 // ----- Extract time
4719 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
4720 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
4721 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
4723 // ----- Extract date
4724 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
4725 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
4726 $v_day = $p_header['mdate'] & 0x001F;
4728 // ----- Get UNIX date format
4729 $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
4731 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
4735 $p_header['mtime'] = time();
4736 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
4740 //for(reset($v_data); $key = key($v_data); next($v_data)) {
4741 // //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Attribut[$key] = ".$v_data[$key]);
4744 // ----- Set the stored filename
4745 $p_header['stored_filename'] = $p_header['filename'];
4747 // ----- Set the status field
4748 $p_header['status'] = "ok";
4751 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4754 // --------------------------------------------------------------------------------
4756 // --------------------------------------------------------------------------------
4757 // Function : privReadCentralFileHeader()
4761 // --------------------------------------------------------------------------------
4762 function privReadCentralFileHeader(&$p_header)
4764 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadCentralFileHeader", "");
4767 // ----- Read the 4 bytes signature
4768 $v_binary_data = @fread($this->zip_fd, 4);
4769 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
4770 $v_data = unpack('Vid', $v_binary_data);
4771 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
4773 // ----- Check signature
4774 if ($v_data['id'] != 0x02014b50)
4776 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid Central Dir File signature");
4779 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
4782 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4783 return PclZip::errorCode();
4786 // ----- Read the first 42 bytes of the header
4787 $v_binary_data = fread($this->zip_fd, 42);
4789 // ----- Look for invalid block size
4790 if (strlen($v_binary_data) != 42)
4792 $p_header['filename'] = "";
4793 $p_header['status'] = "invalid_header";
4794 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
4797 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
4800 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4801 return PclZip::errorCode();
4804 // ----- Extract the values
4805 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header : '".$v_binary_data."'");
4806 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header (Hex) : '".bin2hex($v_binary_data)."'");
4807 $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
4809 // ----- Get filename
4810 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File name length : ".$p_header['filename_len']);
4811 if ($p_header['filename_len'] != 0)
4812 $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
4814 $p_header['filename'] = '';
4815 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Filename : \''.$p_header['filename'].'\'');
4818 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Extra length : ".$p_header['extra_len']);
4819 if ($p_header['extra_len'] != 0)
4820 $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
4822 $p_header['extra'] = '';
4823 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Extra : \''.$p_header['extra'].'\'');
4825 // ----- Get comment
4826 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Comment length : ".$p_header['comment_len']);
4827 if ($p_header['comment_len'] != 0)
4828 $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
4830 $p_header['comment'] = '';
4831 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Comment : \''.$p_header['comment'].'\'');
4833 // ----- Extract properties
4834 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version : \''.($p_header['version']/10).'.'.($p_header['version']%10).'\'');
4835 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
4836 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Size : \''.$p_header['size'].'\'');
4837 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
4838 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
4839 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Flag : \''.$p_header['flag'].'\'');
4840 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Offset : \''.$p_header['offset'].'\'');
4842 // ----- Recuperate date in UNIX format
4843 //if ($p_header['mdate'] && $p_header['mtime'])
4844 // TBC : bug : this was ignoring time with 0/0/0
4847 // ----- Extract time
4848 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
4849 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
4850 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
4852 // ----- Extract date
4853 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
4854 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
4855 $v_day = $p_header['mdate'] & 0x001F;
4857 // ----- Get UNIX date format
4858 $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
4860 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
4864 $p_header['mtime'] = time();
4865 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
4868 // ----- Set the stored filename
4869 $p_header['stored_filename'] = $p_header['filename'];
4871 // ----- Set default status to ok
4872 $p_header['status'] = 'ok';
4874 // ----- Look if it is a directory
4875 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Internal (Hex) : '".sprintf("Ox%04X", $p_header['internal'])."'");
4876 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "External (Hex) : '".sprintf("Ox%04X", $p_header['external'])."' (".(($p_header['external']&0x00000010)==0x00000010?'is a folder':'is a file').')');
4877 if (substr($p_header['filename'], -1) == '/') {
4878 //$p_header['external'] = 0x41FF0010;
4879 $p_header['external'] = 0x00000010;
4880 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Force folder external : \''.sprintf("Ox%04X", $p_header['external']).'\'');
4883 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Header of filename : \''.$p_header['filename'].'\'');
4886 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4889 // --------------------------------------------------------------------------------
4891 // --------------------------------------------------------------------------------
4892 // Function : privCheckFileHeaders()
4898 // --------------------------------------------------------------------------------
4899 function privCheckFileHeaders(&$p_local_header, &$p_central_header)
4901 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFileHeaders", "");
4904 // ----- Check the static values
4906 if ($p_local_header['filename'] != $p_central_header['filename']) {
4907 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename" : TBC To Be Completed');
4909 if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
4910 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "version_extracted" : TBC To Be Completed');
4912 if ($p_local_header['flag'] != $p_central_header['flag']) {
4913 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "flag" : TBC To Be Completed');
4915 if ($p_local_header['compression'] != $p_central_header['compression']) {
4916 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "compression" : TBC To Be Completed');
4918 if ($p_local_header['mtime'] != $p_central_header['mtime']) {
4919 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "mtime" : TBC To Be Completed');
4921 if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
4922 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename_len" : TBC To Be Completed');
4925 // ----- Look for flag bit 3
4926 if (($p_local_header['flag'] & 8) == 8) {
4927 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Purpose bit flag bit 3 set !');
4928 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'File size, compression size and crc found in central header');
4929 $p_local_header['size'] = $p_central_header['size'];
4930 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_local_header['size'].'\'');
4931 $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
4932 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_local_header['compressed_size'].'\'');
4933 $p_local_header['crc'] = $p_central_header['crc'];
4934 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_local_header['crc']).'\'');
4938 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
4941 // --------------------------------------------------------------------------------
4943 // --------------------------------------------------------------------------------
4944 // Function : privReadEndCentralDir()
4948 // --------------------------------------------------------------------------------
4949 function privReadEndCentralDir(&$p_central_dir)
4951 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadEndCentralDir", "");
4954 // ----- Go to the end of the zip file
4955 $v_size = filesize($this->zipname);
4956 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size of the file :$v_size");
4957 @fseek($this->zip_fd, $v_size);
4958 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position at end of zip file : \''.ftell($this->zip_fd).'\'');
4959 if (@ftell($this->zip_fd) != $v_size)
4962 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
4965 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4966 return PclZip::errorCode();
4969 // ----- First try : look if this is an archive with no commentaries (most of the time)
4970 // in this case the end of central dir is at 22 bytes of the file end
4973 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Look for central dir with no comment');
4974 @fseek($this->zip_fd, $v_size-22);
4975 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after min central position : \''.ftell($this->zip_fd).'\'');
4976 if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
4979 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
4982 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
4983 return PclZip::errorCode();
4986 // ----- Read for bytes
4987 $v_binary_data = @fread($this->zip_fd, 4);
4988 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
4989 $v_data = @unpack('Vid', $v_binary_data);
4990 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
4992 // ----- Check signature
4993 if ($v_data['id'] == 0x06054b50) {
4994 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found central dir at the default position.");
4998 $v_pos = ftell($this->zip_fd);
5001 // ----- Go back to the maximum possible size of the Central Dir End Record
5003 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Start extended search of end central dir');
5004 $v_maximum_size = 65557; // 0xFFFF + 22;
5005 if ($v_maximum_size > $v_size)
5006 $v_maximum_size = $v_size;
5007 @fseek($this->zip_fd, $v_size-$v_maximum_size);
5008 if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
5011 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
5014 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5015 return PclZip::errorCode();
5017 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after max central position : \''.ftell($this->zip_fd).'\'');
5019 // ----- Read byte per byte in order to find the signature
5020 $v_pos = ftell($this->zip_fd);
5021 $v_bytes = 0x00000000;
5022 while ($v_pos < $v_size)
5024 // ----- Read a byte
5025 $v_byte = @fread($this->zip_fd, 1);
5027 // ----- Add the byte
5028 // $v_bytes = ($v_bytes << 8) | Ord($v_byte);
5029 // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
5030 // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
5031 $v_bytes = ( ($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
5033 // ----- Compare the bytes
5034 if ($v_bytes == 0x504b0506)
5036 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Found End Central Dir signature at position : \''.ftell($this->zip_fd).'\'');
5044 // ----- Look if not found end of central dir
5045 if ($v_pos == $v_size)
5047 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to find End of Central Dir Record signature");
5050 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
5053 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5054 return PclZip::errorCode();
5058 // ----- Read the first 18 bytes of the header
5059 $v_binary_data = fread($this->zip_fd, 18);
5061 // ----- Look for invalid block size
5062 if (strlen($v_binary_data) != 18)
5064 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
5067 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
5070 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5071 return PclZip::errorCode();
5074 // ----- Extract the values
5075 ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record : '".$v_binary_data."'");
5076 ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record (Hex) : '".bin2hex($v_binary_data)."'");
5077 $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
5079 // ----- Check the global size
5080 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Comment length : ".$v_data['comment_size']);
5081 if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
5082 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The central dir is not at the end of the archive. Some trailing bytes exists after the archive.");
5084 // ----- Removed in release 2.2 see readme file
5085 // The check of the file size is a little too strict.
5086 // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
5087 // While decrypted, zip has training 0 bytes
5090 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
5091 'The central dir is not at the end of the archive.'
5092 .' Some trailing bytes exists after the archive.');
5095 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5096 return PclZip::errorCode();
5100 // ----- Get comment
5101 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment size : \''.$v_data['comment_size'].'\'');
5102 if ($v_data['comment_size'] != 0) {
5103 $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
5106 $p_central_dir['comment'] = '';
5107 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment : \''.$p_central_dir['comment'].'\'');
5109 $p_central_dir['entries'] = $v_data['entries'];
5110 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries : \''.$p_central_dir['entries'].'\'');
5111 $p_central_dir['disk_entries'] = $v_data['disk_entries'];
5112 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries for this disk : \''.$p_central_dir['disk_entries'].'\'');
5113 $p_central_dir['offset'] = $v_data['offset'];
5114 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Offset of Central Dir : \''.$p_central_dir['offset'].'\'');
5115 $p_central_dir['size'] = $v_data['size'];
5116 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size of Central Dir : \''.$p_central_dir['size'].'\'');
5117 $p_central_dir['disk'] = $v_data['disk'];
5118 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Disk number : \''.$p_central_dir['disk'].'\'');
5119 $p_central_dir['disk_start'] = $v_data['disk_start'];
5120 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Start disk number : \''.$p_central_dir['disk_start'].'\'');
5123 //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
5124 // //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "central_dir[$key] = ".$p_central_dir[$key]);
5128 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5131 // --------------------------------------------------------------------------------
5133 // --------------------------------------------------------------------------------
5134 // Function : privDeleteByRule()
5138 // --------------------------------------------------------------------------------
5139 function privDeleteByRule(&$p_result_list, &$p_options)
5141 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDeleteByRule", "");
5143 $v_list_detail = array();
5145 // ----- Open the zip file
5146 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
5147 if (($v_result=$this->privOpenFd('rb')) != 1)
5150 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5154 // ----- Read the central directory informations
5155 $v_central_dir = array();
5156 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
5158 $this->privCloseFd();
5159 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5163 // ----- Go to beginning of File
5164 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
5165 @rewind($this->zip_fd);
5166 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
5168 // ----- Scan all the files
5169 // ----- Start at beginning of Central Dir
5170 $v_pos_entry = $v_central_dir['offset'];
5171 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
5172 @rewind($this->zip_fd);
5173 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
5174 if (@fseek($this->zip_fd, $v_pos_entry))
5176 // ----- Close the zip file
5177 $this->privCloseFd();
5180 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
5183 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5184 return PclZip::errorCode();
5186 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
5188 // ----- Read each entry
5189 $v_header_list = array();
5191 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
5193 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry (index '$i')");
5195 // ----- Read the file header
5196 $v_header_list[$v_nb_extracted] = array();
5197 if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
5199 // ----- Close the zip file
5200 $this->privCloseFd();
5202 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5206 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename (index '$i') : '".$v_header_list[$v_nb_extracted]['stored_filename']."'");
5208 // ----- Store the index
5209 $v_header_list[$v_nb_extracted]['index'] = $i;
5211 // ----- Look for the specific extract rules
5214 // ----- Look for extract by name rule
5215 if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
5216 && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
5217 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
5219 // ----- Look if the filename is in the list
5220 for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
5221 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
5223 // ----- Look for a directory
5224 if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
5225 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
5227 // ----- Look if the directory is in the filename path
5228 if ( (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
5229 && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
5230 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
5233 elseif ( (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
5234 && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
5235 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The entry is the searched directory");
5239 // ----- Look for a filename
5240 elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
5241 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
5247 // ----- Look for extract by ereg rule
5248 else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
5249 && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
5250 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
5252 if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
5253 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
5258 // ----- Look for extract by preg rule
5259 else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
5260 && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
5261 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
5263 if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
5264 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
5269 // ----- Look for extract by index rule
5270 else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
5271 && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
5272 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
5274 // ----- Look if the index is in the list
5275 for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
5276 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look if index '$i' is in [".$p_options[PCLZIP_OPT_BY_INDEX][$j]['start'].",".$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']."]");
5278 if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
5279 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
5282 if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
5283 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
5287 if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
5288 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
5294 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "No argument mean remove all file");
5298 // ----- Look for deletion
5301 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' need to be deleted");
5302 unset($v_header_list[$v_nb_extracted]);
5306 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' will not be deleted");
5311 // ----- Look if something need to be deleted
5312 if ($v_nb_extracted > 0) {
5314 // ----- Creates a temporay file
5315 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
5317 // ----- Creates a temporary zip archive
5318 $v_temp_zip = new PclZip($v_zip_temp_name);
5320 // ----- Open the temporary zip file in write mode
5321 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary write mode");
5322 if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
5323 $this->privCloseFd();
5326 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5330 // ----- Look which file need to be kept
5331 for ($i=0; $i<sizeof($v_header_list); $i++) {
5332 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Keep entry index '$i' : '".$v_header_list[$i]['filename']."'");
5334 // ----- Calculate the position of the header
5335 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset='". $v_header_list[$i]['offset']."'");
5336 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
5337 @rewind($this->zip_fd);
5338 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
5339 if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
5340 // ----- Close the zip file
5341 $this->privCloseFd();
5342 $v_temp_zip->privCloseFd();
5343 @unlink($v_zip_temp_name);
5346 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
5349 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5350 return PclZip::errorCode();
5352 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
5354 // ----- Read the file header
5355 $v_local_header = array();
5356 if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
5357 // ----- Close the zip file
5358 $this->privCloseFd();
5359 $v_temp_zip->privCloseFd();
5360 @unlink($v_zip_temp_name);
5363 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5367 // ----- Check that local file header is same as central file header
5368 if ($this->privCheckFileHeaders($v_local_header,
5369 $v_header_list[$i]) != 1) {
5372 unset($v_local_header);
5374 // ----- Write the file header
5375 if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
5376 // ----- Close the zip file
5377 $this->privCloseFd();
5378 $v_temp_zip->privCloseFd();
5379 @unlink($v_zip_temp_name);
5382 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5385 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset for this file is '".$v_header_list[$i]['offset']."'");
5387 // ----- Read/write the data block
5388 if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
5389 // ----- Close the zip file
5390 $this->privCloseFd();
5391 $v_temp_zip->privCloseFd();
5392 @unlink($v_zip_temp_name);
5395 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5400 // ----- Store the offset of the central dir
5401 $v_offset = @ftell($v_temp_zip->zip_fd);
5402 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "New offset of central dir : $v_offset");
5404 // ----- Re-Create the Central Dir files header
5405 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the new central directory");
5406 for ($i=0; $i<sizeof($v_header_list); $i++) {
5407 // ----- Create the file header
5408 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset of file : ".$v_header_list[$i]['offset']);
5409 if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
5410 $v_temp_zip->privCloseFd();
5411 $this->privCloseFd();
5412 @unlink($v_zip_temp_name);
5415 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5419 // ----- Transform the header to a 'usable' info
5420 $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
5423 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the central directory footer");
5425 // ----- Zip file comment
5427 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
5428 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
5431 // ----- Calculate the size of the central header
5432 $v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
5434 // ----- Create the central dir footer
5435 if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
5436 // ----- Reset the file list
5437 unset($v_header_list);
5438 $v_temp_zip->privCloseFd();
5439 $this->privCloseFd();
5440 @unlink($v_zip_temp_name);
5443 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5448 $v_temp_zip->privCloseFd();
5449 $this->privCloseFd();
5451 // ----- Delete the zip file
5452 // TBC : I should test the result ...
5453 @unlink($this->zipname);
5455 // ----- Rename the temporary file
5456 // TBC : I should test the result ...
5457 //@rename($v_zip_temp_name, $this->zipname);
5458 PclZipUtilRename($v_zip_temp_name, $this->zipname);
5460 // ----- Destroy the temporary archive
5464 // ----- Remove every files : reset the file
5465 else if ($v_central_dir['entries'] != 0) {
5466 $this->privCloseFd();
5468 if (($v_result = $this->privOpenFd('wb')) != 1) {
5469 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5473 if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
5474 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5478 $this->privCloseFd();
5482 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5485 // --------------------------------------------------------------------------------
5487 // --------------------------------------------------------------------------------
5488 // Function : privDirCheck()
5490 // Check if a directory exists, if not it creates it and all the parents directory
5491 // which may be useful.
5493 // $p_dir : Directory path to check.
5496 // -1 : Unable to create directory
5497 // --------------------------------------------------------------------------------
5498 function privDirCheck($p_dir, $p_is_dir=false)
5502 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDirCheck", "entry='$p_dir', is_dir='".($p_is_dir?"true":"false")."'");
5504 // ----- Remove the final '/'
5505 if (($p_is_dir) && (substr($p_dir, -1)=='/'))
5507 $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
5509 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Looking for entry '$p_dir'");
5511 // ----- Check the directory availability
5512 if ((is_dir($p_dir)) || ($p_dir == ""))
5514 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
5518 // ----- Extract parent directory
5519 $p_parent_dir = dirname($p_dir);
5520 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
5522 // ----- Just a check
5523 if ($p_parent_dir != $p_dir)
5525 // ----- Look for parent directory
5526 if ($p_parent_dir != "")
5528 if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
5530 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5536 // ----- Create the directory
5537 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
5538 if (!@mkdir($p_dir, 0777))
5541 PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
5544 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5545 return PclZip::errorCode();
5549 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
5552 // --------------------------------------------------------------------------------
5554 // --------------------------------------------------------------------------------
5555 // Function : privMerge()
5557 // If $p_archive_to_add does not exist, the function exit with a success result.
5560 // --------------------------------------------------------------------------------
5561 function privMerge(&$p_archive_to_add)
5563 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privMerge", "archive='".$p_archive_to_add->zipname."'");
5566 // ----- Look if the archive_to_add exists
5567 if (!is_file($p_archive_to_add->zipname))
5569 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to add does not exist. End of merge.");
5571 // ----- Nothing to merge, so merge is a success
5575 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5579 // ----- Look if the archive exists
5580 if (!is_file($this->zipname))
5582 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, duplicate the archive_to_add.");
5584 // ----- Do a duplicate
5585 $v_result = $this->privDuplicate($p_archive_to_add->zipname);
5588 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5592 // ----- Open the zip file
5593 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
5594 if (($v_result=$this->privOpenFd('rb')) != 1)
5597 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5601 // ----- Read the central directory informations
5602 $v_central_dir = array();
5603 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
5605 $this->privCloseFd();
5606 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5610 // ----- Go to beginning of File
5611 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
5612 @rewind($this->zip_fd);
5613 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
5615 // ----- Open the archive_to_add file
5616 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open archive_to_add in binary read mode");
5617 if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
5619 $this->privCloseFd();
5622 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5626 // ----- Read the central directory informations
5627 $v_central_dir_to_add = array();
5628 if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
5630 $this->privCloseFd();
5631 $p_archive_to_add->privCloseFd();
5633 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5637 // ----- Go to beginning of File
5638 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
5639 @rewind($p_archive_to_add->zip_fd);
5640 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
5642 // ----- Creates a temporay file
5643 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
5645 // ----- Open the temporary file in write mode
5646 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
5647 if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
5649 $this->privCloseFd();
5650 $p_archive_to_add->privCloseFd();
5652 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
5655 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5656 return PclZip::errorCode();
5659 // ----- Copy the files from the archive to the temporary file
5660 // TBC : Here I should better append the file and go back to erase the central dir
5661 $v_size = $v_central_dir['offset'];
5662 while ($v_size != 0)
5664 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5665 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5666 $v_buffer = fread($this->zip_fd, $v_read_size);
5667 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5668 $v_size -= $v_read_size;
5671 // ----- Copy the files from the archive_to_add into the temporary file
5672 $v_size = $v_central_dir_to_add['offset'];
5673 while ($v_size != 0)
5675 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5676 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5677 $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
5678 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5679 $v_size -= $v_read_size;
5682 // ----- Store the offset of the central dir
5683 $v_offset = @ftell($v_zip_temp_fd);
5684 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
5686 // ----- Copy the block of file headers from the old archive
5687 $v_size = $v_central_dir['size'];
5688 while ($v_size != 0)
5690 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5691 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5692 $v_buffer = @fread($this->zip_fd, $v_read_size);
5693 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5694 $v_size -= $v_read_size;
5697 // ----- Copy the block of file headers from the archive_to_add
5698 $v_size = $v_central_dir_to_add['size'];
5699 while ($v_size != 0)
5701 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5702 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
5703 $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
5704 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5705 $v_size -= $v_read_size;
5708 // ----- Merge the file comments
5709 $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
5711 // ----- Calculate the size of the (new) central header
5712 $v_size = @ftell($v_zip_temp_fd)-$v_offset;
5714 // ----- Swap the file descriptor
5715 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
5716 // the following methods on the temporary fil and not the real archive fd
5717 $v_swap = $this->zip_fd;
5718 $this->zip_fd = $v_zip_temp_fd;
5719 $v_zip_temp_fd = $v_swap;
5721 // ----- Create the central dir footer
5722 if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
5724 $this->privCloseFd();
5725 $p_archive_to_add->privCloseFd();
5726 @fclose($v_zip_temp_fd);
5727 $this->zip_fd = null;
5729 // ----- Reset the file list
5730 unset($v_header_list);
5733 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5737 // ----- Swap back the file descriptor
5738 $v_swap = $this->zip_fd;
5739 $this->zip_fd = $v_zip_temp_fd;
5740 $v_zip_temp_fd = $v_swap;
5743 $this->privCloseFd();
5744 $p_archive_to_add->privCloseFd();
5746 // ----- Close the temporary file
5747 @fclose($v_zip_temp_fd);
5749 // ----- Delete the zip file
5750 // TBC : I should test the result ...
5751 @unlink($this->zipname);
5753 // ----- Rename the temporary file
5754 // TBC : I should test the result ...
5755 //@rename($v_zip_temp_name, $this->zipname);
5756 PclZipUtilRename($v_zip_temp_name, $this->zipname);
5759 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5762 // --------------------------------------------------------------------------------
5764 // --------------------------------------------------------------------------------
5765 // Function : privDuplicate()
5769 // --------------------------------------------------------------------------------
5770 function privDuplicate($p_archive_filename)
5772 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDuplicate", "archive_filename='$p_archive_filename'");
5775 // ----- Look if the $p_archive_filename exists
5776 if (!is_file($p_archive_filename))
5778 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to duplicate does not exist. End of duplicate.");
5780 // ----- Nothing to duplicate, so duplicate is a success.
5784 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5788 // ----- Open the zip file
5789 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
5790 if (($v_result=$this->privOpenFd('wb')) != 1)
5793 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5797 // ----- Open the temporary file in write mode
5798 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
5799 if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
5801 $this->privCloseFd();
5803 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
5806 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
5807 return PclZip::errorCode();
5810 // ----- Copy the files from the archive to the temporary file
5811 // TBC : Here I should better append the file and go back to erase the central dir
5812 $v_size = filesize($p_archive_filename);
5813 while ($v_size != 0)
5815 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5816 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read $v_read_size bytes");
5817 $v_buffer = fread($v_zip_temp_fd, $v_read_size);
5818 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
5819 $v_size -= $v_read_size;
5823 $this->privCloseFd();
5825 // ----- Close the temporary file
5826 @fclose($v_zip_temp_fd);
5829 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5832 // --------------------------------------------------------------------------------
5834 // --------------------------------------------------------------------------------
5835 // Function : privErrorLog()
5838 // --------------------------------------------------------------------------------
5839 function privErrorLog($p_error_code=0, $p_error_string='')
5841 if (PCLZIP_ERROR_EXTERNAL == 1) {
5842 PclError($p_error_code, $p_error_string);
5845 $this->error_code = $p_error_code;
5846 $this->error_string = $p_error_string;
5849 // --------------------------------------------------------------------------------
5851 // --------------------------------------------------------------------------------
5852 // Function : privErrorReset()
5855 // --------------------------------------------------------------------------------
5856 function privErrorReset()
5858 if (PCLZIP_ERROR_EXTERNAL == 1) {
5862 $this->error_code = 0;
5863 $this->error_string = '';
5866 // --------------------------------------------------------------------------------
5868 // --------------------------------------------------------------------------------
5869 // Function : privDisableMagicQuotes()
5873 // --------------------------------------------------------------------------------
5874 function privDisableMagicQuotes()
5876 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privDisableMagicQuotes', "");
5879 // ----- Look if function exists
5880 if ( (!function_exists("get_magic_quotes_runtime"))
5881 || (!function_exists("set_magic_quotes_runtime"))) {
5882 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Functions *et_magic_quotes_runtime are not supported");
5883 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5887 // ----- Look if already done
5888 if ($this->magic_quotes_status != -1) {
5889 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "magic_quote already disabled");
5890 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5894 // ----- Get and memorize the magic_quote value
5895 $this->magic_quotes_status = @get_magic_quotes_runtime();
5896 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Current magic_quotes_runtime status is '".($this->magic_quotes_status==0?'disable':'enable')."'");
5898 // ----- Disable magic_quotes
5899 if ($this->magic_quotes_status == 1) {
5900 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Disable magic_quotes");
5901 @set_magic_quotes_runtime(0);
5905 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5908 // --------------------------------------------------------------------------------
5910 // --------------------------------------------------------------------------------
5911 // Function : privSwapBackMagicQuotes()
5915 // --------------------------------------------------------------------------------
5916 function privSwapBackMagicQuotes()
5918 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privSwapBackMagicQuotes', "");
5921 // ----- Look if function exists
5922 if ( (!function_exists("get_magic_quotes_runtime"))
5923 || (!function_exists("set_magic_quotes_runtime"))) {
5924 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Functions *et_magic_quotes_runtime are not supported");
5925 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5929 // ----- Look if something to do
5930 if ($this->magic_quotes_status != -1) {
5931 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "magic_quote not modified");
5932 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5936 // ----- Swap back magic_quotes
5937 if ($this->magic_quotes_status == 1) {
5938 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Enable back magic_quotes");
5939 @set_magic_quotes_runtime($this->magic_quotes_status);
5943 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
5946 // --------------------------------------------------------------------------------
5950 // --------------------------------------------------------------------------------
5952 // --------------------------------------------------------------------------------
5953 // Function : PclZipUtilPathReduction()
5957 // --------------------------------------------------------------------------------
5958 function PclZipUtilPathReduction($p_dir)
5960 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathReduction", "dir='$p_dir'");
5963 // ----- Look for not empty path
5965 // ----- Explode path by directory names
5966 $v_list = explode("/", $p_dir);
5968 // ----- Study directories from last to first
5970 for ($i=sizeof($v_list)-1; $i>=0; $i--) {
5971 // ----- Look for current path
5972 if ($v_list[$i] == ".") {
5973 // ----- Ignore this directory
5974 // Should be the first $i=0, but no check is done
5976 else if ($v_list[$i] == "..") {
5979 else if ($v_list[$i] == "") {
5980 // ----- First '/' i.e. root slash
5982 $v_result = "/".$v_result;
5984 // ----- It is an invalid path, so the path is not modified
5987 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid path is unchanged");
5991 // ----- Last '/' i.e. indicates a directory
5992 else if ($i == (sizeof($v_list)-1)) {
5993 $v_result = $v_list[$i];
5995 // ----- Double '/' inside the path
5997 // ----- Ignore only the double '//' in path,
5998 // but not the first and last '/'
6002 // ----- Look for item to skip
6007 $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
6012 // ----- Look for skip
6014 while ($v_skip > 0) {
6015 $v_result = '../'.$v_result;
6022 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
6025 // --------------------------------------------------------------------------------
6027 // --------------------------------------------------------------------------------
6028 // Function : PclZipUtilPathInclusion()
6030 // This function indicates if the path $p_path is under the $p_dir tree. Or,
6031 // said in an other way, if the file or sub-dir $p_path is inside the dir
6033 // The function indicates also if the path is exactly the same as the dir.
6034 // This function supports path with duplicated '/' like '//', but does not
6035 // support '.' or '..' statements.
6038 // 0 if $p_path is not inside directory $p_dir
6039 // 1 if $p_path is inside directory $p_dir
6040 // 2 if $p_path is exactly the same as $p_dir
6041 // --------------------------------------------------------------------------------
6042 function PclZipUtilPathInclusion($p_dir, $p_path)
6044 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathInclusion", "dir='$p_dir', path='$p_path'");
6047 // ----- Look for path beginning by ./
6048 if ( ($p_dir == '.')
6049 || ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
6050 $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
6051 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Replacing ./ by full path in p_dir '".$p_dir."'");
6053 if ( ($p_path == '.')
6054 || ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
6055 $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
6056 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Replacing ./ by full path in p_path '".$p_path."'");
6059 // ----- Explode dir and path by directory separator
6060 $v_list_dir = explode("/", $p_dir);
6061 $v_list_dir_size = sizeof($v_list_dir);
6062 $v_list_path = explode("/", $p_path);
6063 $v_list_path_size = sizeof($v_list_path);
6065 // ----- Study directories paths
6068 while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
6069 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Working on dir($i)='".$v_list_dir[$i]."' and path($j)='".$v_list_path[$j]."'");
6071 // ----- Look for empty dir (path reduction)
6072 if ($v_list_dir[$i] == '') {
6076 if ($v_list_path[$j] == '') {
6081 // ----- Compare the items
6082 if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) {
6083 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Items ($i,$j) are different");
6092 // ----- Look if everything seems to be the same
6094 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Look for tie break");
6095 // ----- Skip all the empty items
6096 while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
6097 while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
6098 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Looking on dir($i)='".($i < $v_list_dir_size?$v_list_dir[$i]:'')."' and path($j)='".($j < $v_list_path_size?$v_list_path[$j]:'')."'");
6100 if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
6101 // ----- There are exactly the same
6104 else if ($i < $v_list_dir_size) {
6105 // ----- The path is shorter than the dir
6111 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
6114 // --------------------------------------------------------------------------------
6116 // --------------------------------------------------------------------------------
6117 // Function : PclZipUtilCopyBlock()
6120 // $p_mode : read/write compression mode
6121 // 0 : src & dest normal
6122 // 1 : src gzip, dest normal
6123 // 2 : src normal, dest gzip
6124 // 3 : src & dest gzip
6126 // --------------------------------------------------------------------------------
6127 function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
6129 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilCopyBlock", "size=$p_size, mode=$p_mode");
6134 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset before read :".(@ftell($p_src)));
6135 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset before write :".(@ftell($p_dest)));
6136 while ($p_size != 0)
6138 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
6139 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
6140 $v_buffer = @fread($p_src, $v_read_size);
6141 @fwrite($p_dest, $v_buffer, $v_read_size);
6142 $p_size -= $v_read_size;
6144 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset after read :".(@ftell($p_src)));
6145 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset after write :".(@ftell($p_dest)));
6147 else if ($p_mode==1)
6149 while ($p_size != 0)
6151 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
6152 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
6153 $v_buffer = @gzread($p_src, $v_read_size);
6154 @fwrite($p_dest, $v_buffer, $v_read_size);
6155 $p_size -= $v_read_size;
6158 else if ($p_mode==2)
6160 while ($p_size != 0)
6162 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
6163 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
6164 $v_buffer = @fread($p_src, $v_read_size);
6165 @gzwrite($p_dest, $v_buffer, $v_read_size);
6166 $p_size -= $v_read_size;
6169 else if ($p_mode==3)
6171 while ($p_size != 0)
6173 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
6174 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
6175 $v_buffer = @gzread($p_src, $v_read_size);
6176 @gzwrite($p_dest, $v_buffer, $v_read_size);
6177 $p_size -= $v_read_size;
6182 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
6185 // --------------------------------------------------------------------------------
6187 // --------------------------------------------------------------------------------
6188 // Function : PclZipUtilRename()
6190 // This function tries to do a simple rename() function. If it fails, it
6191 // tries to copy the $p_src file in a new $p_dest file and then unlink the
6194 // $p_src : Old filename
6195 // $p_dest : New filename
6197 // 1 on success, 0 on failure.
6198 // --------------------------------------------------------------------------------
6199 function PclZipUtilRename($p_src, $p_dest)
6201 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilRename", "source=$p_src, destination=$p_dest");
6204 // ----- Try to rename the files
6205 if (!@rename($p_src, $p_dest)) {
6206 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to rename file, try copy+unlink");
6208 // ----- Try to copy & unlink the src
6209 if (!@copy($p_src, $p_dest)) {
6210 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to copy file");
6213 else if (!@unlink($p_src)) {
6214 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to unlink old filename");
6220 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
6223 // --------------------------------------------------------------------------------
6225 // --------------------------------------------------------------------------------
6226 // Function : PclZipUtilOptionText()
6228 // Translate option value in text. Mainly for debug purpose.
6230 // $p_option : the option value.
6232 // The option text value.
6233 // --------------------------------------------------------------------------------
6234 function PclZipUtilOptionText($p_option)
6236 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilOptionText", "option='".$p_option."'");
6238 $v_list = get_defined_constants();
6239 for (reset($v_list); $v_key = key($v_list); next($v_list)) {
6240 $v_prefix = substr($v_key, 0, 10);
6241 if (( ($v_prefix == 'PCLZIP_OPT')
6242 || ($v_prefix == 'PCLZIP_CB_')
6243 || ($v_prefix == 'PCLZIP_ATT'))
6244 && ($v_list[$v_key] == $p_option)) {
6245 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_key);
6250 $v_result = 'Unknown';
6252 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
6255 // --------------------------------------------------------------------------------
6257 // --------------------------------------------------------------------------------
6258 // Function : PclZipUtilTranslateWinPath()
6260 // Translate windows path by replacing '\' by '/' and optionally removing
6263 // $p_path : path to translate.
6264 // $p_remove_disk_letter : true | false
6266 // The path translated.
6267 // --------------------------------------------------------------------------------
6268 function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
6270 if (stristr(php_uname(), 'windows')) {
6271 // ----- Look for potential disk letter
6272 if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
6273 $p_path = substr($p_path, $v_position+1);
6275 // ----- Change potential windows directory separator
6276 if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
6277 $p_path = strtr($p_path, '\\', '/');
6282 // --------------------------------------------------------------------------------