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