Fixing content type ordering when content_type is not defined.
[akelos.git] / lib / AkZip.php
blob5917045b2be987ae93449c006caafe02b0481f53
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3 // +----------------------------------------------------------------------+
4 // | Akelos Framework - http://www.akelos.org |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
7 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
8 // +----------------------------------------------------------------------+
10 /**
11 * @package ActiveSupport
12 * @subpackage Utils
13 * @author Bermi Ferrer <bermi a.t akelos c.om>
14 * @author Vincent Blavet <vincent@blavet.net>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
20 require_once 'PEAR.php';
21 // ----- Constants
22 define('AK_ZIP_READ_BLOCK_SIZE', 2048);
23 // ----- File list separator
24 define('AK_ZIP_SEPARATOR', ',');
25 // ----- Optional static temporary directory
26 // By default temporary files are generated in the script current
27 // path.
28 // If defined :
29 // - MUST BE terminated by a '/'.
30 // - MUST be a valid, already created directory
31 // Samples :
32 // define( 'AK_ZIP_TEMPORARY_DIR', '/temp/' );
33 // define( 'AK_ZIP_TEMPORARY_DIR', 'C:/Temp/' );
34 define('AK_ZIP_TEMPORARY_DIR', '');
35 // ----- Error codes
36 define('AK_ZIP_ERR_NO_ERROR', 0);
37 define('AK_ZIP_ERR_WRITE_OPEN_FAIL', -1);
38 define('AK_ZIP_ERR_READ_OPEN_FAIL', -2);
39 define('AK_ZIP_ERR_INVALID_PARAMETER', -3);
40 define('AK_ZIP_ERR_MISSING_FILE', -4);
41 define('AK_ZIP_ERR_FILENAME_TOO_LONG', -5);
42 define('AK_ZIP_ERR_INVALID_ZIP', -6);
43 define('AK_ZIP_ERR_BAD_EXTRACTED_FILE', -7);
44 define('AK_ZIP_ERR_DIR_CREATE_FAIL', -8);
45 define('AK_ZIP_ERR_BAD_EXTENSION', -9);
46 define('AK_ZIP_ERR_BAD_FORMAT', -10);
47 define('AK_ZIP_ERR_DELETE_FILE_FAIL', -11);
48 define('AK_ZIP_ERR_RENAME_FILE_FAIL', -12);
49 define('AK_ZIP_ERR_BAD_CHECKSUM', -13);
50 define('AK_ZIP_ERR_INVALID_AK_ZIP', -14);
51 define('AK_ZIP_ERR_MISSING_OPTION_VALUE', -15);
52 define('AK_ZIP_ERR_INVALID_PARAM_VALUE', -16);
53 // ----- Warning codes
54 define('AK_ZIP_WARN_NO_WARNING', 0);
55 define('AK_ZIP_WARN_FILE_EXIST', 1);
56 // ----- Methods parameters
57 define('AK_ZIP_PARAM_PATH', 'path');
58 define('AK_ZIP_PARAM_ADD_PATH', 'add_path');
59 define('AK_ZIP_PARAM_REMOVE_PATH', 'remove_path');
60 define('AK_ZIP_PARAM_REMOVE_ALL_PATH', 'remove_all_path');
61 define('AK_ZIP_PARAM_SET_CHMOD', 'set_chmod');
62 define('AK_ZIP_PARAM_EXTRACT_AS_STRING', 'extract_as_string');
63 define('AK_ZIP_PARAM_NO_COMPRESSION', 'no_compression');
64 define('AK_ZIP_PARAM_BY_NAME', 'by_name');
65 define('AK_ZIP_PARAM_BY_INDEX', 'by_index');
66 define('AK_ZIP_PARAM_BY_EREG', 'by_ereg');
67 define('AK_ZIP_PARAM_BY_PREG', 'by_preg');
68 define('AK_ZIP_PARAM_PRE_EXTRACT', 'callback_pre_extract');
69 define('AK_ZIP_PARAM_POST_EXTRACT', 'callback_post_extract');
70 define('AK_ZIP_PARAM_PRE_ADD', 'callback_pre_add');
71 define('AK_ZIP_PARAM_POST_ADD', 'callback_post_add');
72 /**
73 * Class for manipulating zip archive files
75 * A class which provided common methods to manipulate ZIP formatted
76 * archive files.
77 * It provides creation, extraction, deletion and add features.
79 * @author Vincent Blavet <vincent@blavet.net>
80 * @version $Revision: 1.6 $
81 * @category Archive
84 class AkZip
86 /**
87 * The filename of the zip archive.
89 * @var string Name of the Zip file
91 var $_zipname = '';
92 /**
93 * File descriptor of the opened Zip file.
95 * @var int Internal zip file descriptor
97 var $_zip_fd = 0;
98 /**
99 * @var int last error code
101 var $_error_code = 1;
103 * @var string Last error description
105 var $_error_string = '';
106 // {{{ constructor
109 * AkZip Class constructor. This flavour of the constructor only
110 * declare a new AkZip object, identifying it by the name of the
111 * zip file.
113 * @param string $p_zipname The name of the zip archive to create
114 * @access public
117 function AkZip($p_zipname)
119 // ----- Check the zlib
120 if (!extension_loaded('zlib')) {
121 PEAR::loadExtension('zlib');
123 if (!extension_loaded('zlib')) {
124 die("The extension 'zlib' couldn't be found.\n"."Please make sure your version of PHP was built "."with 'zlib' support.\n");
125 return false;
127 // ----- Set the attributes
128 $this->_zipname = $p_zipname;
129 $this->_zip_fd = 0;
130 return;
132 // }}}
133 // {{{ create()
136 * This method creates a Zip Archive with the filename set with
137 * the constructor.
138 * The files and directories indicated in $p_filelist
139 * are added in the archive.
140 * When a directory is in the list, the directory and its content is added
141 * in the archive.
142 * The methods takes a variable list of parameters in $p_params.
143 * The supported parameters for this method are :
144 * 'add_path' : Add a path to the archived files.
145 * 'remove_path' : Remove the specified 'root' path of the archived files.
146 * 'remove_all_path' : Remove all the path of the archived files.
147 * 'no_compression' : The archived files will not be compressed.
149 * @access public
150 * @param mixed $p_filelist The list of the files or folders to add.
151 * It can be a string with filenames separated
152 * by a comma, or an array of filenames.
153 * @param mixed $p_params An array of variable parameters and values.
154 * @return mixed An array of file description on success,
155 * an error code on error
158 function create($p_filelist, $p_params = 0)
160 $this->_errorReset();
161 // ----- Set default values
162 if ($p_params === 0) {
163 $p_params = array();
165 if ($this->_check_parameters($p_params, array(
166 'no_compression' => false,
167 'add_path' => "",
168 'remove_path' => "",
169 'remove_all_path' => false
170 )) != 1) {
171 return 0;
173 // ----- Look if the $p_filelist is really an array
174 $p_result_list = array();
175 if (is_array($p_filelist)) {
176 $v_result = $this->_create($p_filelist, $p_result_list, $p_params);
178 // ----- Look if the $p_filelist is a string
179 else if (is_string($p_filelist)) {
180 // ----- Create a list with the elements from the string
181 $v_list = explode(AK_ZIP_SEPARATOR, $p_filelist);
182 $v_result = $this->_create($v_list, $p_result_list, $p_params);
184 // ----- Invalid variable
185 else {
186 $this->_errorLog(AK_ZIP_ERR_INVALID_PARAMETER, 'Invalid variable type p_filelist');
187 $v_result = AK_ZIP_ERR_INVALID_PARAMETER;
189 if ($v_result != 1) {
190 return 0;
192 return $p_result_list;
194 // }}}
195 // {{{ add()
198 * This method add files or directory in an existing Zip Archive.
199 * If the Zip Archive does not exist it is created.
200 * The files and directories to add are indicated in $p_filelist.
201 * When a directory is in the list, the directory and its content is added
202 * in the archive.
203 * The methods takes a variable list of parameters in $p_params.
204 * The supported parameters for this method are :
205 * 'add_path' : Add a path to the archived files.
206 * 'remove_path' : Remove the specified 'root' path of the archived files.
207 * 'remove_all_path' : Remove all the path of the archived files.
208 * 'no_compression' : The archived files will not be compressed.
209 * 'callback_pre_add' : A callback function that will be called before
210 * each entry archiving.
211 * 'callback_post_add' : A callback function that will be called after
212 * each entry archiving.
214 * @access public
215 * @param mixed $p_filelist The list of the files or folders to add.
216 * It can be a string with filenames separated
217 * by a comma, or an array of filenames.
218 * @param mixed $p_params An array of variable parameters and values.
219 * @return mixed An array of file description on success,
220 * 0 on an unrecoverable failure, an error code is logged.
223 function add($p_filelist, $p_params = 0)
225 $this->_errorReset();
226 // ----- Set default values
227 if ($p_params === 0) {
228 $p_params = array();
230 if ($this->_check_parameters($p_params, array(
231 'no_compression' => false,
232 'add_path' => '',
233 'remove_path' => '',
234 'remove_all_path' => false,
235 'callback_pre_add' => '',
236 'callback_post_add' => ''
237 )) != 1) {
238 return 0;
240 // ----- Look if the $p_filelist is really an array
241 $p_result_list = array();
242 if (is_array($p_filelist)) {
243 // ----- Call the create fct
244 $v_result = $this->_add($p_filelist, $p_result_list, $p_params);
246 // ----- Look if the $p_filelist is a string
247 else if (is_string($p_filelist)) {
248 // ----- Create a list with the elements from the string
249 $v_list = explode(AK_ZIP_SEPARATOR, $p_filelist);
250 // ----- Call the create fct
251 $v_result = $this->_add($v_list, $p_result_list, $p_params);
253 // ----- Invalid variable
254 else {
255 $this->_errorLog(AK_ZIP_ERR_INVALID_PARAMETER, "add() : Invalid variable type p_filelist");
256 $v_result = AK_ZIP_ERR_INVALID_PARAMETER;
258 if ($v_result != 1) {
259 return 0;
261 // ----- Return the result list
262 return $p_result_list;
264 // }}}
265 // {{{ listContent()
268 * This method gives the names and properties of the files and directories
269 * which are present in the zip archive.
270 * The properties of each entries in the list are :
271 * filename : Name of the file.
272 * For create() or add() it's the filename given by the user.
273 * For an extract() it's the filename of the extracted file.
274 * stored_filename : Name of the file / directory stored in the archive.
275 * size : Size of the stored file.
276 * compressed_size : Size of the file's data compressed in the archive
277 * (without the zip headers overhead)
278 * mtime : Last known modification date of the file (UNIX timestamp)
279 * comment : Comment associated with the file
280 * folder : true | false (indicates if the entry is a folder)
281 * index : index of the file in the archive (-1 when not available)
282 * status : status of the action on the entry (depending of the action) :
283 * Values are :
284 * ok : OK !
285 * filtered : the file/dir was not extracted (filtered by user)
286 * already_a_directory : the file can't be extracted because a
287 * directory with the same name already
288 * exists
289 * write_protected : the file can't be extracted because a file
290 * with the same name already exists and is
291 * write protected
292 * newer_exist : the file was not extracted because a newer
293 * file already exists
294 * path_creation_fail : the file is not extracted because the
295 * folder does not exists and can't be
296 * created
297 * write_error : the file was not extracted because there was a
298 * error while writing the file
299 * read_error : the file was not extracted because there was a
300 * error while reading the file
301 * invalid_header : the file was not extracted because of an
302 * archive format error (bad file header)
303 * Note that each time a method can continue operating when there
304 * is an error on a single file, the error is only logged in the file status.
306 * @access public
307 * @return mixed An array of file description on success,
308 * 0 on an unrecoverable failure, an error code is logged.
311 function listContent()
313 $this->_errorReset();
314 // ----- Check archive
315 if (!$this->_checkFormat()) {
316 return (0);
318 $v_list = array();
319 if ($this->_list($v_list) != 1) {
320 unset($v_list);
321 return (0);
323 return $v_list;
325 // }}}
326 // {{{ extract()
329 * This method extract the files and folders which are in the zip archive.
330 * It can extract all the archive or a part of the archive by using filter
331 * feature (extract by name, by index, by ereg, by preg). The extraction
332 * can occur in the current path or an other path.
333 * All the advanced features are activated by the use of variable
334 * parameters.
335 * The return value is an array of entry descriptions which gives
336 * information on extracted files (See listContent()).
337 * The method may return a success value (an array) even if some files
338 * are not correctly extracted (see the file status in listContent()).
339 * The supported variable parameters for this method are :
340 * 'add_path' : Path where the files and directories are to be extracted
341 * 'remove_path' : First part ('root' part) of the memorized path
342 * (if similar) to remove while extracting.
343 * 'remove_all_path' : Remove all the memorized path while extracting.
344 * 'extract_as_string' :
345 * 'set_chmod' : After the extraction of the file the indicated mode
346 * will be set.
347 * 'by_name' : It can be a string with file/dir names separated by ',',
348 * or an array of file/dir names to extract from the archive.
349 * 'by_index' : A string with range of indexes separated by ',',
350 * (sample "1,3-5,12").
351 * 'by_ereg' : A regular expression (ereg) that must match the extracted
352 * filename.
353 * 'by_preg' : A regular expression (preg) that must match the extracted
354 * filename.
355 * 'callback_pre_extract' : A callback function that will be called before
356 * each entry extraction.
357 * 'callback_post_extract' : A callback function that will be called after
358 * each entry extraction.
360 * @access public
361 * @param mixed $p_params An array of variable parameters and values.
362 * @return mixed An array of file description on success,
363 * 0 on an unrecoverable failure, an error code is logged.
366 function extract($p_params = 0)
368 $this->_errorReset();
369 // ----- Check archive
370 if (!$this->_checkFormat()) {
371 return (0);
373 // ----- Set default values
374 if ($p_params === 0) {
375 $p_params = array();
377 if ($this->_check_parameters($p_params, array(
378 'extract_as_string' => false,
379 'add_path' => '',
380 'remove_path' => '',
381 'remove_all_path' => false,
382 'callback_pre_extract' => '',
383 'callback_post_extract' => '',
384 'set_chmod' => 0,
385 'by_name' => '',
386 'by_index' => '',
387 'by_ereg' => '',
388 'by_preg' => ''
389 )) != 1) {
390 return 0;
392 // ----- Call the extracting fct
393 $v_list = array();
394 if ($this->_extractByRule($v_list, $p_params) != 1) {
395 unset($v_list);
396 return (0);
398 return $v_list;
400 // }}}
401 // {{{ delete()
404 * This methods delete archive entries in the zip archive.
405 * Notice that at least one filtering rule (set by the variable parameter
406 * list) must be set.
407 * Also notice that if you delete a folder entry, only the folder entry
408 * is deleted, not all the files bellonging to this folder.
409 * The supported variable parameters for this method are :
410 * 'by_name' : It can be a string with file/dir names separated by ',',
411 * or an array of file/dir names to delete from the archive.
412 * 'by_index' : A string with range of indexes separated by ',',
413 * (sample "1,3-5,12").
414 * 'by_ereg' : A regular expression (ereg) that must match the extracted
415 * filename.
416 * 'by_preg' : A regular expression (preg) that must match the extracted
417 * filename.
419 * @access public
420 * @param mixed $p_params An array of variable parameters and values.
421 * @return mixed An array of file description on success,
422 * 0 on an unrecoverable failure, an error code is logged.
425 function delete($p_params)
427 $this->_errorReset();
428 // ----- Check archive
429 if (!$this->_checkFormat()) {
430 return (0);
432 // ----- Set default values
433 if ($this->_check_parameters($p_params, array(
434 'by_name' => '',
435 'by_index' => '',
436 'by_ereg' => '',
437 'by_preg' => ''
438 )) != 1) {
439 return 0;
441 // ----- Check that at least one rule is set
442 if (($p_params['by_name'] == '') && ($p_params['by_index'] == '') && ($p_params['by_ereg'] == '') && ($p_params['by_preg'] == '')) {
443 $this->_errorLog(AK_ZIP_ERR_INVALID_PARAMETER, 'At least one filtering rule must'.' be set as parameter');
444 return 0;
446 // ----- Call the delete fct
447 $v_list = array();
448 if ($this->_deleteByRule($v_list, $p_params) != 1) {
449 unset($v_list);
450 return (0);
452 return $v_list;
454 // }}}
455 // {{{ properties()
458 * This method gives the global properties of the archive.
459 * The properties are :
460 * nb : Number of files in the archive
461 * comment : Comment associated with the archive file
462 * status : not_exist, ok
464 * @access public
465 * @param mixed $p_params {Description}
466 * @return mixed An array with the global properties or 0 on error.
469 function properties()
471 $this->_errorReset();
472 // ----- Check archive
473 if (!$this->_checkFormat()) {
474 return (0);
476 // ----- Default properties
477 $v_prop = array();
478 $v_prop['comment'] = '';
479 $v_prop['nb'] = 0;
480 $v_prop['status'] = 'not_exist';
481 // ----- Look if file exists
482 if (@is_file($this->_zipname)) {
483 // ----- Open the zip file
484 if (($this->_zip_fd = @fopen($this->_zipname, 'rb')) == 0) {
485 $this->_errorLog(AK_ZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->_zipname.'\' in binary read mode');
486 return 0;
488 // ----- Read the central directory informations
489 $v_central_dir = array();
490 if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
491 return 0;
493 $this->_closeFd();
494 // ----- Set the user attributes
495 $v_prop['comment'] = $v_central_dir['comment'];
496 $v_prop['nb'] = $v_central_dir['entries'];
497 $v_prop['status'] = 'ok';
499 return $v_prop;
501 // }}}
502 // {{{ duplicate()
505 * This method creates an archive by copying the content of an other one.
506 * If the archive already exist, it is replaced by the new one without
507 * any warning.
509 * @access public
510 * @param mixed $p_archive It can be a valid AkZip object or
511 * the filename of a valid zip archive.
512 * @return integer 1 on success, 0 on failure.
515 function duplicate($p_archive)
517 $this->_errorReset();
518 // ----- Look if the $p_archive is a AkZip object
519 if ((is_object($p_archive)) && (strtolower(get_class($p_archive)) == 'archive_zip')) {
520 $v_result = $this->_duplicate($p_archive->_zipname);
522 // ----- Look if the $p_archive is a string (so a filename)
523 else if (is_string($p_archive)) {
524 // ----- Check that $p_archive is a valid zip file
525 // TBC : Should also check the archive format
526 if (!is_file($p_archive)) {
527 $this->_errorLog(AK_ZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
528 $v_result = AK_ZIP_ERR_MISSING_FILE;
529 } else {
530 $v_result = $this->_duplicate($p_archive);
533 // ----- Invalid variable
534 else {
535 $this->_errorLog(AK_ZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
536 $v_result = AK_ZIP_ERR_INVALID_PARAMETER;
538 return $v_result;
540 // }}}
541 // {{{ merge()
544 * This method merge a valid zip archive at the end of the
545 * archive identified by the AkZip object.
546 * If the archive ($this) does not exist, the merge becomes a duplicate.
547 * If the archive to add does not exist, the merge is a success.
549 * @access public
550 * @param mixed $p_archive_to_add It can be a valid AkZip object or
551 * the filename of a valid zip archive.
552 * @return integer 1 on success, 0 on failure.
555 function merge($p_archive_to_add)
557 $v_result = 1;
558 $this->_errorReset();
559 // ----- Check archive
560 if (!$this->_checkFormat()) {
561 return (0);
563 // ----- Look if the $p_archive_to_add is a AkZip object
564 if ((is_object($p_archive_to_add)) && (strtolower(get_class($p_archive_to_add)) == 'archive_zip')) {
565 $v_result = $this->_merge($p_archive_to_add);
567 // ----- Look if the $p_archive_to_add is a string (so a filename)
568 else if (is_string($p_archive_to_add)) {
569 // ----- Create a temporary archive
570 $v_object_archive = new AkZip($p_archive_to_add);
571 // ----- Merge the archive
572 $v_result = $this->_merge($v_object_archive);
574 // ----- Invalid variable
575 else {
576 $this->_errorLog(AK_ZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
577 $v_result = AK_ZIP_ERR_INVALID_PARAMETER;
579 return $v_result;
581 // }}}
582 // {{{ errorCode()
585 * Method that gives the lastest error code.
587 * @access public
588 * @return integer The error code value.
591 function errorCode()
593 return ($this->_error_code);
595 // }}}
596 // {{{ errorName()
599 * This method gives the latest error code name.
601 * @access public
602 * @param boolean $p_with_code If true, gives the name and the int value.
603 * @return string The error name.
606 function errorName($p_with_code = false)
608 $v_const_list = get_defined_constants();
609 // ----- Extract error constants from all const.
610 for (reset($v_const_list) ; list($v_key, $v_value) = each($v_const_list) ;) {
611 if (substr($v_key, 0, strlen('AK_ZIP_ERR_')) == 'AK_ZIP_ERR_') {
612 $v_error_list[$v_key] = $v_value;
615 // ----- Search the name form the code value
616 $v_key = array_search($this->_error_code, $v_error_list, true);
617 if ($v_key != false) {
618 $v_value = $v_key;
619 } else {
620 $v_value = 'NoName';
622 if ($p_with_code) {
623 return ($v_value.' ('.$this->_error_code.')');
624 } else {
625 return ($v_value);
628 // }}}
629 // {{{ errorInfo()
632 * This method returns the description associated with the latest error.
634 * @access public
635 * @param boolean $p_full If set to true gives the description with the
636 * error code, the name and the description.
637 * If set to false gives only the description
638 * and the error code.
639 * @return string The error description.
642 function errorInfo($p_full = false)
644 if ($p_full) {
645 return ($this->errorName(true) ." : ".$this->_error_string);
646 } else {
647 return ($this->_error_string." [code ".$this->_error_code."]");
650 // }}}
651 // -----------------------------------------------------------------------------
652 // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
653 // ***** *****
654 // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
655 // -----------------------------------------------------------------------------
656 // ---------------------------------------------------------------------------
657 // Function : _checkFormat()
658 // Description :
659 // This method check that the archive exists and is a valid zip archive.
660 // Several level of check exists. (futur)
661 // Parameters :
662 // $p_level : Level of check. Default 0.
663 // 0 : Check the first bytes (magic codes) (default value))
664 // 1 : 0 + Check the central directory (futur)
665 // 2 : 1 + Check each file header (futur)
666 // Return Values :
667 // true on success,
668 // false on error, the error code is set.
669 // ---------------------------------------------------------------------------
672 * AkZip::_checkFormat()
674 * { Description }
676 * @param integer $p_level
679 function _checkFormat($p_level = 0)
681 $v_result = true;
682 // ----- Reset the error handler
683 $this->_errorReset();
684 // ----- Look if the file exits
685 if (!is_file($this->_zipname)) {
686 // ----- Error log
687 $this->_errorLog(AK_ZIP_ERR_MISSING_FILE, "Missing archive file '".$this->_zipname."'");
688 return (false);
690 // ----- Check that the file is readeable
691 if (!is_readable($this->_zipname)) {
692 // ----- Error log
693 $this->_errorLog(AK_ZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->_zipname."'");
694 return (false);
696 // ----- Check the magic code
697 // TBC
698 // ----- Check the central header
699 // TBC
700 // ----- Check each file header
701 // TBC
702 // ----- Return
703 return $v_result;
705 // ---------------------------------------------------------------------------
706 // ---------------------------------------------------------------------------
707 // Function : _create()
708 // Description :
709 // Parameters :
710 // Return Values :
711 // ---------------------------------------------------------------------------
714 * AkZip::_create()
716 * { Description }
720 function _create($p_list, &$p_result_list, &$p_params)
722 $v_result = 1;
723 $v_list_detail = array();
724 $p_add_dir = $p_params['add_path'];
725 $p_remove_dir = $p_params['remove_path'];
726 $p_remove_all_dir = $p_params['remove_all_path'];
727 // ----- Open the file in write mode
728 if (($v_result = $this->_openFd('wb')) != 1) {
729 // ----- Return
730 return $v_result;
732 // ----- Add the list of files
733 $v_result = $this->_addList($p_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params);
734 // ----- Close
735 $this->_closeFd();
736 // ----- Return
737 return $v_result;
739 // ---------------------------------------------------------------------------
740 // ---------------------------------------------------------------------------
741 // Function : _add()
742 // Description :
743 // Parameters :
744 // Return Values :
745 // ---------------------------------------------------------------------------
748 * AkZip::_add()
750 * { Description }
754 function _add($p_list, &$p_result_list, &$p_params)
756 $v_result = 1;
757 $v_list_detail = array();
758 $p_add_dir = $p_params['add_path'];
759 $p_remove_dir = $p_params['remove_path'];
760 $p_remove_all_dir = $p_params['remove_all_path'];
761 // ----- Look if the archive exists or is empty and need to be created
762 if ((!is_file($this->_zipname)) || (filesize($this->_zipname) == 0)) {
763 $v_result = $this->_create($p_list, $p_result_list, $p_params);
764 return $v_result;
766 // ----- Open the zip file
767 if (($v_result = $this->_openFd('rb')) != 1) {
768 return $v_result;
770 // ----- Read the central directory informations
771 $v_central_dir = array();
772 if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
773 $this->_closeFd();
774 return $v_result;
776 // ----- Go to beginning of File
777 @rewind($this->_zip_fd);
778 // ----- Creates a temporay file
779 $v_zip_temp_name = AK_ZIP_TEMPORARY_DIR.uniqid('archive_zip-') .'.tmp';
780 // ----- Open the temporary file in write mode
781 if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
782 $this->_closeFd();
783 $this->_errorLog(AK_ZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
784 return AkZip::errorCode();
786 // ----- Copy the files from the archive to the temporary file
787 // TBC : Here I should better append the file and go back to erase the
788 // central dir
789 $v_size = $v_central_dir['offset'];
790 while ($v_size != 0) {
791 $v_read_size = ($v_size < AK_ZIP_READ_BLOCK_SIZE ? $v_size : AK_ZIP_READ_BLOCK_SIZE);
792 $v_buffer = fread($this->_zip_fd, $v_read_size);
793 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
794 $v_size-= $v_read_size;
796 // ----- Swap the file descriptor
797 // Here is a trick : I swap the temporary fd with the zip fd, in order to
798 // use the following methods on the temporary fil and not the real archive
799 $v_swap = $this->_zip_fd;
800 $this->_zip_fd = $v_zip_temp_fd;
801 $v_zip_temp_fd = $v_swap;
802 // ----- Add the files
803 $v_header_list = array();
804 if (($v_result = $this->_addFileList($p_list, $v_header_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params)) != 1) {
805 fclose($v_zip_temp_fd);
806 $this->_closeFd();
807 @unlink($v_zip_temp_name);
808 // ----- Return
809 return $v_result;
811 // ----- Store the offset of the central dir
812 $v_offset = @ftell($this->_zip_fd);
813 // ----- Copy the block of file headers from the old archive
814 $v_size = $v_central_dir['size'];
815 while ($v_size != 0) {
816 $v_read_size = ($v_size < AK_ZIP_READ_BLOCK_SIZE ? $v_size : AK_ZIP_READ_BLOCK_SIZE);
817 $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
818 @fwrite($this->_zip_fd, $v_buffer, $v_read_size);
819 $v_size-= $v_read_size;
821 // ----- Create the Central Dir files header
822 for ($i = 0, $v_count = 0 ; $i < sizeof($v_header_list) ; $i++) {
823 // ----- Create the file header
824 if ($v_header_list[$i]['status'] == 'ok') {
825 if (($v_result = $this->_writeCentralFileHeader($v_header_list[$i])) != 1) {
826 fclose($v_zip_temp_fd);
827 $this->_closeFd();
828 @unlink($v_zip_temp_name);
829 // ----- Return
830 return $v_result;
832 $v_count++;
834 // ----- Transform the header to a 'usable' info
835 $this->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
837 // ----- Zip file comment
838 $v_comment = '';
839 // ----- Calculate the size of the central header
840 $v_size = @ftell($this->_zip_fd) -$v_offset;
841 // ----- Create the central dir footer
842 if (($v_result = $this->_writeCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1) {
843 // ----- Reset the file list
844 unset($v_header_list);
845 // ----- Return
846 return $v_result;
848 // ----- Swap back the file descriptor
849 $v_swap = $this->_zip_fd;
850 $this->_zip_fd = $v_zip_temp_fd;
851 $v_zip_temp_fd = $v_swap;
852 // ----- Close
853 $this->_closeFd();
854 // ----- Close the temporary file
855 @fclose($v_zip_temp_fd);
856 // ----- Delete the zip file
857 // TBC : I should test the result ...
858 @unlink($this->_zipname);
859 // ----- Rename the temporary file
860 // TBC : I should test the result ...
861 //@rename($v_zip_temp_name, $this->_zipname);
862 $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
863 // ----- Return
864 return $v_result;
866 // ---------------------------------------------------------------------------
867 // ---------------------------------------------------------------------------
868 // Function : _openFd()
869 // Description :
870 // Parameters :
871 // ---------------------------------------------------------------------------
874 * AkZip::_openFd()
876 * { Description }
880 function _openFd($p_mode)
882 $v_result = 1;
883 // ----- Look if already open
884 if ($this->_zip_fd != 0) {
885 $this->_errorLog(AK_ZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->_zipname.'\' already open');
886 return AkZip::errorCode();
888 // ----- Open the zip file
889 if (($this->_zip_fd = @fopen($this->_zipname, $p_mode)) == 0) {
890 $this->_errorLog(AK_ZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->_zipname.'\' in '.$p_mode.' mode');
891 return AkZip::errorCode();
893 // ----- Return
894 return $v_result;
896 // ---------------------------------------------------------------------------
897 // ---------------------------------------------------------------------------
898 // Function : _closeFd()
899 // Description :
900 // Parameters :
901 // ---------------------------------------------------------------------------
904 * AkZip::_closeFd()
906 * { Description }
910 function _closeFd()
912 $v_result = 1;
913 if ($this->_zip_fd != 0) @fclose($this->_zip_fd);
914 $this->_zip_fd = 0;
915 // ----- Return
916 return $v_result;
918 // ---------------------------------------------------------------------------
919 // ---------------------------------------------------------------------------
920 // Function : _addList()
921 // Description :
922 // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
923 // different from the real path of the file. This is usefull if you want to have PclTar
924 // running in any directory, and memorize relative path from an other directory.
925 // Parameters :
926 // $p_list : An array containing the file or directory names to add in the tar
927 // $p_result_list : list of added files with their properties (specially the status field)
928 // $p_add_dir : Path to add in the filename path archived
929 // $p_remove_dir : Path to remove in the filename path archived
930 // Return Values :
931 // ---------------------------------------------------------------------------
934 * AkZip::_addList()
936 * { Description }
940 function _addList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_params)
942 $v_result = 1;
943 // ----- Add the files
944 $v_header_list = array();
945 if (($v_result = $this->_addFileList($p_list, $v_header_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params)) != 1) {
946 return $v_result;
948 // ----- Store the offset of the central dir
949 $v_offset = @ftell($this->_zip_fd);
950 // ----- Create the Central Dir files header
951 for ($i = 0, $v_count = 0 ; $i < sizeof($v_header_list) ; $i++) {
952 // ----- Create the file header
953 if ($v_header_list[$i]['status'] == 'ok') {
954 if (($v_result = $this->_writeCentralFileHeader($v_header_list[$i])) != 1) {
955 return $v_result;
957 $v_count++;
959 // ----- Transform the header to a 'usable' info
960 $this->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
962 // ----- Zip file comment
963 $v_comment = '';
964 // ----- Calculate the size of the central header
965 $v_size = @ftell($this->_zip_fd) -$v_offset;
966 // ----- Create the central dir footer
967 if (($v_result = $this->_writeCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1) {
968 // ----- Reset the file list
969 unset($v_header_list);
970 // ----- Return
971 return $v_result;
973 // ----- Return
974 return $v_result;
976 // ---------------------------------------------------------------------------
977 // ---------------------------------------------------------------------------
978 // Function : _addFileList()
979 // Description :
980 // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
981 // different from the real path of the file. This is usefull if you want to
982 // run the lib in any directory, and memorize relative path from an other directory.
983 // Parameters :
984 // $p_list : An array containing the file or directory names to add in the tar
985 // $p_result_list : list of added files with their properties (specially the status field)
986 // $p_add_dir : Path to add in the filename path archived
987 // $p_remove_dir : Path to remove in the filename path archived
988 // Return Values :
989 // ---------------------------------------------------------------------------
992 * AkZip::_addFileList()
994 * { Description }
998 function _addFileList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_params)
1000 $v_result = 1;
1001 $v_header = array();
1002 // ----- Recuperate the current number of elt in list
1003 $v_nb = sizeof($p_result_list);
1004 // ----- Loop on the files
1005 for ($j = 0 ; ($j < count($p_list)) && ($v_result == 1) ; $j++) {
1006 // ----- Recuperate the filename
1007 $p_filename = $this->_tool_TranslateWinPath($p_list[$j], false);
1008 // ----- Skip empty file names
1009 if ($p_filename == "") {
1010 continue;
1012 // ----- Check the filename
1013 if (!file_exists($p_filename)) {
1014 $this->_errorLog(AK_ZIP_ERR_MISSING_FILE, "File '$p_filename' does not exists");
1015 return AkZip::errorCode();
1017 // ----- Look if it is a file or a dir with no all pathnre move
1018 if ((is_file($p_filename)) || ((is_dir($p_filename)) && !$p_remove_all_dir)) {
1019 // ----- Add the file
1020 if (($v_result = $this->_addFile($p_filename, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params)) != 1) {
1021 // ----- Return status
1022 return $v_result;
1024 // ----- Store the file infos
1025 $p_result_list[$v_nb++] = $v_header;
1027 // ----- Look for directory
1028 if (is_dir($p_filename)) {
1029 // ----- Look for path
1030 if ($p_filename != ".") $v_path = $p_filename."/";
1031 else $v_path = "";
1032 // ----- Read the directory for files and sub-directories
1033 $p_hdir = opendir($p_filename);
1034 $p_hitem = readdir($p_hdir); // '.' directory
1035 $p_hitem = readdir($p_hdir); // '..' directory
1036 while ($p_hitem = readdir($p_hdir)) {
1037 // ----- Look for a file
1038 if (is_file($v_path.$p_hitem)) {
1039 // ----- Add the file
1040 if (($v_result = $this->_addFile($v_path.$p_hitem, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params)) != 1) {
1041 // ----- Return status
1042 return $v_result;
1044 // ----- Store the file infos
1045 $p_result_list[$v_nb++] = $v_header;
1047 // ----- Recursive call to _addFileList()
1048 else {
1049 // ----- Need an array as parameter
1050 $p_temp_list[0] = $v_path.$p_hitem;
1051 $v_result = $this->_addFileList($p_temp_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params);
1052 // ----- Update the number of elements of the list
1053 $v_nb = sizeof($p_result_list);
1056 // ----- Free memory for the recursive loop
1057 unset($p_temp_list);
1058 unset($p_hdir);
1059 unset($p_hitem);
1062 return $v_result;
1064 // ---------------------------------------------------------------------------
1065 // ---------------------------------------------------------------------------
1066 // Function : _addFile()
1067 // Description :
1068 // Parameters :
1069 // Return Values :
1070 // ---------------------------------------------------------------------------
1073 * AkZip::_addFile()
1075 * { Description }
1079 function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_params)
1081 $v_result = 1;
1082 if ($p_filename == "") {
1083 // ----- Error log
1084 $this->_errorLog(AK_ZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
1085 // ----- Return
1086 return AkZip::errorCode();
1088 // ----- Calculate the stored filename
1089 $v_stored_filename = $p_filename;
1090 // ----- Look for all path to remove
1091 if ($p_remove_all_dir) {
1092 $v_stored_filename = basename($p_filename);
1094 // ----- Look for partial path remove
1095 else if ($p_remove_dir != "") {
1096 if (substr($p_remove_dir, -1) != '/') $p_remove_dir.= "/";
1097 if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./")) {
1098 if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./")) $p_remove_dir = "./".$p_remove_dir;
1099 if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./")) $p_remove_dir = substr($p_remove_dir, 2);
1101 $v_compare = $this->_tool_PathInclusion($p_remove_dir, $p_filename);
1102 if ($v_compare > 0)
1103 // if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
1105 if ($v_compare == 2) {
1106 $v_stored_filename = "";
1107 } else {
1108 $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
1112 // ----- Look for path to add
1113 if ($p_add_dir != "") {
1114 if (substr($p_add_dir, -1) == "/") $v_stored_filename = $p_add_dir.$v_stored_filename;
1115 else $v_stored_filename = $p_add_dir."/".$v_stored_filename;
1117 // ----- Filename (reduce the path of stored name)
1118 $v_stored_filename = $this->_tool_PathReduction($v_stored_filename);
1119 /* filename length moved after call-back in release 1.3
1120 // ----- Check the path length
1121 if (strlen($v_stored_filename) > 0xFF)
1123 // ----- Error log
1124 $this->_errorLog(-5, "Stored file name is too long (max. 255) : '$v_stored_filename'");
1126 // ----- Return
1127 return AkZip::errorCode();
1130 // ----- Set the file properties
1131 clearstatcache();
1132 $p_header['version'] = 20;
1133 $p_header['version_extracted'] = 10;
1134 $p_header['flag'] = 0;
1135 $p_header['compression'] = 0;
1136 $p_header['mtime'] = filemtime($p_filename);
1137 $p_header['crc'] = 0;
1138 $p_header['compressed_size'] = 0;
1139 $p_header['size'] = filesize($p_filename);
1140 $p_header['filename_len'] = strlen($p_filename);
1141 $p_header['extra_len'] = 0;
1142 $p_header['comment_len'] = 0;
1143 $p_header['disk'] = 0;
1144 $p_header['internal'] = 0;
1145 $p_header['external'] = (is_file($p_filename) ? 0xFE49FFE0 : 0x41FF0010);
1146 $p_header['offset'] = 0;
1147 $p_header['filename'] = $p_filename;
1148 $p_header['stored_filename'] = $v_stored_filename;
1149 $p_header['extra'] = '';
1150 $p_header['comment'] = '';
1151 $p_header['status'] = 'ok';
1152 $p_header['index'] = -1;
1153 // ----- Look for pre-add callback
1154 if ((isset($p_params[AK_ZIP_PARAM_PRE_ADD])) && ($p_params[AK_ZIP_PARAM_PRE_ADD] != '')) {
1155 // ----- Generate a local information
1156 $v_local_header = array();
1157 $this->_convertHeader2FileInfo($p_header, $v_local_header);
1158 // ----- Call the callback
1159 // Here I do not use call_user_func() because I need to send a reference to the
1160 // header.
1161 eval('$v_result = '.$p_params[AK_ZIP_PARAM_PRE_ADD].'(AK_ZIP_PARAM_PRE_ADD, $v_local_header);');
1162 if ($v_result == 0) {
1163 // ----- Change the file status
1164 $p_header['status'] = "skipped";
1165 $v_result = 1;
1167 // ----- Update the informations
1168 // Only some fields can be modified
1169 if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
1170 $p_header['stored_filename'] = $this->_tool_PathReduction($v_local_header['stored_filename']);
1173 // ----- Look for empty stored filename
1174 if ($p_header['stored_filename'] == "") {
1175 $p_header['status'] = "filtered";
1177 // ----- Check the path length
1178 if (strlen($p_header['stored_filename']) > 0xFF) {
1179 $p_header['status'] = 'filename_too_long';
1181 // ----- Look if no error, or file not skipped
1182 if ($p_header['status'] == 'ok') {
1183 // ----- Look for a file
1184 if (is_file($p_filename)) {
1185 // ----- Open the source file
1186 if (($v_file = @fopen($p_filename, "rb")) == 0) {
1187 $this->_errorLog(AK_ZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
1188 return AkZip::errorCode();
1190 if ($p_params['no_compression']) {
1191 // ----- Read the file content
1192 $v_content_compressed = @fread($v_file, $p_header['size']);
1193 // ----- Calculate the CRC
1194 $p_header['crc'] = crc32($v_content_compressed);
1195 } else {
1196 // ----- Read the file content
1197 $v_content = @fread($v_file, $p_header['size']);
1198 // ----- Calculate the CRC
1199 $p_header['crc'] = crc32($v_content);
1200 // ----- Compress the file
1201 $v_content_compressed = gzdeflate($v_content);
1203 // ----- Set header parameters
1204 $p_header['compressed_size'] = strlen($v_content_compressed);
1205 $p_header['compression'] = 8;
1206 // ----- Call the header generation
1207 if (($v_result = $this->_writeFileHeader($p_header)) != 1) {
1208 @fclose($v_file);
1209 return $v_result;
1211 // ----- Write the compressed content
1212 $v_binary_data = pack('a'.$p_header['compressed_size'], $v_content_compressed);
1213 @fwrite($this->_zip_fd, $v_binary_data, $p_header['compressed_size']);
1214 // ----- Close the file
1215 @fclose($v_file);
1217 // ----- Look for a directory
1218 else {
1219 // ----- Set the file properties
1220 $p_header['filename'].= '/';
1221 $p_header['filename_len']++;
1222 $p_header['size'] = 0;
1223 $p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
1224 // ----- Call the header generation
1225 if (($v_result = $this->_writeFileHeader($p_header)) != 1) {
1226 return $v_result;
1230 // ----- Look for pre-add callback
1231 if ((isset($p_params[AK_ZIP_PARAM_POST_ADD])) && ($p_params[AK_ZIP_PARAM_POST_ADD] != '')) {
1232 // ----- Generate a local information
1233 $v_local_header = array();
1234 $this->_convertHeader2FileInfo($p_header, $v_local_header);
1235 // ----- Call the callback
1236 // Here I do not use call_user_func() because I need to send a reference to the
1237 // header.
1238 eval('$v_result = '.$p_params[AK_ZIP_PARAM_POST_ADD].'(AK_ZIP_PARAM_POST_ADD, $v_local_header);');
1239 if ($v_result == 0) {
1240 // ----- Ignored
1241 $v_result = 1;
1243 // ----- Update the informations
1244 // Nothing can be modified
1247 // ----- Return
1248 return $v_result;
1250 // ---------------------------------------------------------------------------
1251 // ---------------------------------------------------------------------------
1252 // Function : _writeFileHeader()
1253 // Description :
1254 // Parameters :
1255 // Return Values :
1256 // ---------------------------------------------------------------------------
1259 * AkZip::_writeFileHeader()
1261 * { Description }
1265 function _writeFileHeader(&$p_header)
1267 $v_result = 1;
1268 // TBC
1269 //for(reset($p_header); $key = key($p_header); next($p_header)) {
1271 // ----- Store the offset position of the file
1272 $p_header['offset'] = ftell($this->_zip_fd);
1273 // ----- Transform UNIX mtime to DOS format mdate/mtime
1274 $v_date = getdate($p_header['mtime']);
1275 $v_mtime = ($v_date['hours']<<11) +($v_date['minutes']<<5) +$v_date['seconds']/2;
1276 $v_mdate = (($v_date['year']-1980) <<9) +($v_date['mon']<<5) +$v_date['mday'];
1277 // ----- Packed data
1278 $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50, $p_header['version'], $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'], $p_header['compressed_size'], $p_header['size'], strlen($p_header['stored_filename']) , $p_header['extra_len']);
1279 // ----- Write the first 148 bytes of the header in the archive
1280 fputs($this->_zip_fd, $v_binary_data, 30);
1281 // ----- Write the variable fields
1282 if (strlen($p_header['stored_filename']) != 0) {
1283 fputs($this->_zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
1285 if ($p_header['extra_len'] != 0) {
1286 fputs($this->_zip_fd, $p_header['extra'], $p_header['extra_len']);
1288 // ----- Return
1289 return $v_result;
1291 // ---------------------------------------------------------------------------
1292 // ---------------------------------------------------------------------------
1293 // Function : _writeCentralFileHeader()
1294 // Description :
1295 // Parameters :
1296 // Return Values :
1297 // ---------------------------------------------------------------------------
1300 * AkZip::_writeCentralFileHeader()
1302 * { Description }
1306 function _writeCentralFileHeader(&$p_header)
1308 $v_result = 1;
1309 // TBC
1310 //for(reset($p_header); $key = key($p_header); next($p_header)) {
1312 // ----- Transform UNIX mtime to DOS format mdate/mtime
1313 $v_date = getdate($p_header['mtime']);
1314 $v_mtime = ($v_date['hours']<<11) +($v_date['minutes']<<5) +$v_date['seconds']/2;
1315 $v_mdate = (($v_date['year']-1980) <<9) +($v_date['mon']<<5) +$v_date['mday'];
1316 // ----- Packed data
1317 $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50, $p_header['version'], $p_header['version_extracted'], $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'], $p_header['compressed_size'], $p_header['size'], strlen($p_header['stored_filename']) , $p_header['extra_len'], $p_header['comment_len'], $p_header['disk'], $p_header['internal'], $p_header['external'], $p_header['offset']);
1318 // ----- Write the 42 bytes of the header in the zip file
1319 fputs($this->_zip_fd, $v_binary_data, 46);
1320 // ----- Write the variable fields
1321 if (strlen($p_header['stored_filename']) != 0) {
1322 fputs($this->_zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
1324 if ($p_header['extra_len'] != 0) {
1325 fputs($this->_zip_fd, $p_header['extra'], $p_header['extra_len']);
1327 if ($p_header['comment_len'] != 0) {
1328 fputs($this->_zip_fd, $p_header['comment'], $p_header['comment_len']);
1330 // ----- Return
1331 return $v_result;
1333 // ---------------------------------------------------------------------------
1334 // ---------------------------------------------------------------------------
1335 // Function : _writeCentralHeader()
1336 // Description :
1337 // Parameters :
1338 // Return Values :
1339 // ---------------------------------------------------------------------------
1342 * AkZip::_writeCentralHeader()
1344 * { Description }
1348 function _writeCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
1350 $v_result = 1;
1351 // ----- Packed data
1352 $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries, $p_nb_entries, $p_size, $p_offset, strlen($p_comment));
1353 // ----- Write the 22 bytes of the header in the zip file
1354 fputs($this->_zip_fd, $v_binary_data, 22);
1355 // ----- Write the variable fields
1356 if (strlen($p_comment) != 0) {
1357 fputs($this->_zip_fd, $p_comment, strlen($p_comment));
1359 // ----- Return
1360 return $v_result;
1362 // ---------------------------------------------------------------------------
1363 // ---------------------------------------------------------------------------
1364 // Function : _list()
1365 // Description :
1366 // Parameters :
1367 // Return Values :
1368 // ---------------------------------------------------------------------------
1371 * AkZip::_list()
1373 * { Description }
1377 function _list(&$p_list)
1379 $v_result = 1;
1380 // ----- Open the zip file
1381 if (($this->_zip_fd = @fopen($this->_zipname, 'rb')) == 0) {
1382 // ----- Error log
1383 $this->_errorLog(AK_ZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->_zipname.'\' in binary read mode');
1384 // ----- Return
1385 return AkZip::errorCode();
1387 // ----- Read the central directory informations
1388 $v_central_dir = array();
1389 if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
1390 return $v_result;
1392 // ----- Go to beginning of Central Dir
1393 @rewind($this->_zip_fd);
1394 if (@fseek($this->_zip_fd, $v_central_dir['offset'])) {
1395 // ----- Error log
1396 $this->_errorLog(AK_ZIP_ERR_INVALID_AK_ZIP, 'Invalid archive size');
1397 // ----- Return
1398 return AkZip::errorCode();
1400 // ----- Read each entry
1401 for ($i = 0 ; $i < $v_central_dir['entries'] ; $i++) {
1402 // ----- Read the file header
1403 if (($v_result = $this->_readCentralFileHeader($v_header)) != 1) {
1404 return $v_result;
1406 $v_header['index'] = $i;
1407 // ----- Get the only interesting attributes
1408 $this->_convertHeader2FileInfo($v_header, $p_list[$i]);
1409 unset($v_header);
1411 // ----- Close the zip file
1412 $this->_closeFd();
1413 // ----- Return
1414 return $v_result;
1416 // ---------------------------------------------------------------------------
1417 // ---------------------------------------------------------------------------
1418 // Function : _convertHeader2FileInfo()
1419 // Description :
1420 // This function takes the file informations from the central directory
1421 // entries and extract the interesting parameters that will be given back.
1422 // The resulting file infos are set in the array $p_info
1423 // $p_info['filename'] : Filename with full path. Given by user (add),
1424 // extracted in the filesystem (extract).
1425 // $p_info['stored_filename'] : Stored filename in the archive.
1426 // $p_info['size'] = Size of the file.
1427 // $p_info['compressed_size'] = Compressed size of the file.
1428 // $p_info['mtime'] = Last modification date of the file.
1429 // $p_info['comment'] = Comment associated with the file.
1430 // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
1431 // $p_info['status'] = status of the action on the file.
1432 // Parameters :
1433 // Return Values :
1434 // ---------------------------------------------------------------------------
1437 * AkZip::_convertHeader2FileInfo()
1439 * { Description }
1443 function _convertHeader2FileInfo($p_header, &$p_info)
1445 $v_result = 1;
1446 // ----- Get the interesting attributes
1447 $p_info['filename'] = $p_header['filename'];
1448 $p_info['stored_filename'] = $p_header['stored_filename'];
1449 $p_info['size'] = $p_header['size'];
1450 $p_info['compressed_size'] = $p_header['compressed_size'];
1451 $p_info['mtime'] = $p_header['mtime'];
1452 $p_info['comment'] = $p_header['comment'];
1453 $p_info['folder'] = (($p_header['external']&0x00000010) == 0x00000010);
1454 $p_info['index'] = $p_header['index'];
1455 $p_info['status'] = $p_header['status'];
1456 // ----- Return
1457 return $v_result;
1459 // ---------------------------------------------------------------------------
1460 // ---------------------------------------------------------------------------
1461 // Function : _extractByRule()
1462 // Description :
1463 // Extract a file or directory depending of rules (by index, by name, ...)
1464 // Parameters :
1465 // $p_file_list : An array where will be placed the properties of each
1466 // extracted file
1467 // $p_path : Path to add while writing the extracted files
1468 // $p_remove_path : Path to remove (from the file memorized path) while writing the
1469 // extracted files. If the path does not match the file path,
1470 // the file is extracted with its memorized path.
1471 // $p_remove_path does not apply to 'list' mode.
1472 // $p_path and $p_remove_path are commulative.
1473 // Return Values :
1474 // 1 on success,0 or less on error (see error code list)
1475 // ---------------------------------------------------------------------------
1478 * AkZip::_extractByRule()
1480 * { Description }
1484 function _extractByRule(&$p_file_list, &$p_params)
1486 $v_result = 1;
1487 $p_path = $p_params['add_path'];
1488 $p_remove_path = $p_params['remove_path'];
1489 $p_remove_all_path = $p_params['remove_all_path'];
1490 // ----- Check the path
1491 if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../") && (substr($p_path, 1, 2) != ":/"))) $p_path = "./".$p_path;
1492 // ----- Reduce the path last (and duplicated) '/'
1493 if (($p_path != "./") && ($p_path != "/")) {
1494 // ----- Look for the path end '/'
1495 while (substr($p_path, -1) == "/") {
1496 $p_path = substr($p_path, 0, strlen($p_path) -1);
1499 // ----- Look for path to remove format (should end by /)
1500 if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/')) {
1501 $p_remove_path.= '/';
1503 $p_remove_path_size = strlen($p_remove_path);
1504 // ----- Open the zip file
1505 if (($v_result = $this->_openFd('rb')) != 1) {
1506 return $v_result;
1508 // ----- Read the central directory informations
1509 $v_central_dir = array();
1510 if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
1511 // ----- Close the zip file
1512 $this->_closeFd();
1513 return $v_result;
1515 // ----- Start at beginning of Central Dir
1516 $v_pos_entry = $v_central_dir['offset'];
1517 // ----- Read each entry
1518 $j_start = 0;
1519 for ($i = 0, $v_nb_extracted = 0 ; $i < $v_central_dir['entries'] ; $i++) {
1520 // ----- Read next Central dir entry
1521 @rewind($this->_zip_fd);
1522 if (@fseek($this->_zip_fd, $v_pos_entry)) {
1523 $this->_closeFd();
1524 $this->_errorLog(AK_ZIP_ERR_INVALID_AK_ZIP, 'Invalid archive size');
1525 return AkZip::errorCode();
1527 // ----- Read the file header
1528 $v_header = array();
1529 if (($v_result = $this->_readCentralFileHeader($v_header)) != 1) {
1530 $this->_closeFd();
1531 return $v_result;
1533 // ----- Store the index
1534 $v_header['index'] = $i;
1535 // ----- Store the file position
1536 $v_pos_entry = ftell($this->_zip_fd);
1537 // ----- Look for the specific extract rules
1538 $v_extract = false;
1539 // ----- Look for extract by name rule
1540 if ((isset($p_params[AK_ZIP_PARAM_BY_NAME])) && ($p_params[AK_ZIP_PARAM_BY_NAME] != 0)) {
1541 // ----- Look if the filename is in the list
1542 for ($j = 0 ; ($j < sizeof($p_params[AK_ZIP_PARAM_BY_NAME])) && (!$v_extract) ; $j++) {
1543 // ----- Look for a directory
1544 if (substr($p_params[AK_ZIP_PARAM_BY_NAME][$j], -1) == "/") {
1545 // ----- Look if the directory is in the filename path
1546 if ((strlen($v_header['stored_filename']) > strlen($p_params[AK_ZIP_PARAM_BY_NAME][$j])) && (substr($v_header['stored_filename'], 0, strlen($p_params[AK_ZIP_PARAM_BY_NAME][$j])) == $p_params[AK_ZIP_PARAM_BY_NAME][$j])) {
1547 $v_extract = true;
1550 // ----- Look for a filename
1551 elseif ($v_header['stored_filename'] == $p_params[AK_ZIP_PARAM_BY_NAME][$j]) {
1552 $v_extract = true;
1556 // ----- Look for extract by ereg rule
1557 else if ((isset($p_params[AK_ZIP_PARAM_BY_EREG])) && ($p_params[AK_ZIP_PARAM_BY_EREG] != "")) {
1558 if (ereg($p_params[AK_ZIP_PARAM_BY_EREG], $v_header['stored_filename'])) {
1559 $v_extract = true;
1562 // ----- Look for extract by preg rule
1563 else if ((isset($p_params[AK_ZIP_PARAM_BY_PREG])) && ($p_params[AK_ZIP_PARAM_BY_PREG] != "")) {
1564 if (preg_match($p_params[AK_ZIP_PARAM_BY_PREG], $v_header['stored_filename'])) {
1565 $v_extract = true;
1568 // ----- Look for extract by index rule
1569 else if ((isset($p_params[AK_ZIP_PARAM_BY_INDEX])) && ($p_params[AK_ZIP_PARAM_BY_INDEX] != 0)) {
1570 // ----- Look if the index is in the list
1571 for ($j = $j_start ; ($j < sizeof($p_params[AK_ZIP_PARAM_BY_INDEX])) && (!$v_extract) ; $j++) {
1572 if (($i >= $p_params[AK_ZIP_PARAM_BY_INDEX][$j]['start']) && ($i <= $p_params[AK_ZIP_PARAM_BY_INDEX][$j]['end'])) {
1573 $v_extract = true;
1575 if ($i >= $p_params[AK_ZIP_PARAM_BY_INDEX][$j]['end']) {
1576 $j_start = $j+1;
1578 if ($p_params[AK_ZIP_PARAM_BY_INDEX][$j]['start'] > $i) {
1579 break;
1583 // ----- Look for no rule, which means extract all the archive
1584 else {
1585 $v_extract = true;
1587 // ----- Look for real extraction
1588 if ($v_extract) {
1589 // ----- Go to the file position
1590 @rewind($this->_zip_fd);
1591 if (@fseek($this->_zip_fd, $v_header['offset'])) {
1592 // ----- Close the zip file
1593 $this->_closeFd();
1594 // ----- Error log
1595 $this->_errorLog(AK_ZIP_ERR_INVALID_AK_ZIP, 'Invalid archive size');
1596 // ----- Return
1597 return AkZip::errorCode();
1599 // ----- Look for extraction as string
1600 if ($p_params[AK_ZIP_PARAM_EXTRACT_AS_STRING]) {
1601 // ----- Extracting the file
1602 if (($v_result = $this->_extractFileAsString($v_header, $v_string)) != 1) {
1603 // ----- Close the zip file
1604 $this->_closeFd();
1605 return $v_result;
1607 // ----- Get the only interesting attributes
1608 if (($v_result = $this->_convertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1) {
1609 // ----- Close the zip file
1610 $this->_closeFd();
1611 return $v_result;
1613 // ----- Set the file content
1614 $p_file_list[$v_nb_extracted]['content'] = $v_string;
1615 // ----- Next extracted file
1616 $v_nb_extracted++;
1617 } else {
1618 // ----- Extracting the file
1619 if (($v_result = $this->_extractFile($v_header, $p_path, $p_remove_path, $p_remove_all_path, $p_params)) != 1) {
1620 // ----- Close the zip file
1621 $this->_closeFd();
1622 return $v_result;
1624 // ----- Get the only interesting attributes
1625 if (($v_result = $this->_convertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
1626 // ----- Close the zip file
1627 $this->_closeFd();
1628 return $v_result;
1633 // ----- Close the zip file
1634 $this->_closeFd();
1635 // ----- Return
1636 return $v_result;
1638 // ---------------------------------------------------------------------------
1639 // ---------------------------------------------------------------------------
1640 // Function : _extractFile()
1641 // Description :
1642 // Parameters :
1643 // Return Values :
1644 // ---------------------------------------------------------------------------
1647 * AkZip::_extractFile()
1649 * { Description }
1653 function _extractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_params)
1655 $v_result = 1;
1656 // ----- Read the file header
1657 if (($v_result = $this->_readFileHeader($v_header)) != 1) {
1658 // ----- Return
1659 return $v_result;
1661 // ----- Check that the file header is coherent with $p_entry info
1662 // TBC
1663 // ----- Look for all path to remove
1664 if ($p_remove_all_path == true) {
1665 // ----- Get the basename of the path
1666 $p_entry['filename'] = basename($p_entry['filename']);
1668 // ----- Look for path to remove
1669 else if ($p_remove_path != "") {
1670 //if (strcmp($p_remove_path, $p_entry['filename'])==0)
1671 if ($this->_tool_PathInclusion($p_remove_path, $p_entry['filename']) == 2) {
1672 // ----- Change the file status
1673 $p_entry['status'] = "filtered";
1674 // ----- Return
1675 return $v_result;
1677 $p_remove_path_size = strlen($p_remove_path);
1678 if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
1679 // ----- Remove the path
1680 $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
1683 // ----- Add the path
1684 if ($p_path != '') {
1685 $p_entry['filename'] = $p_path."/".$p_entry['filename'];
1687 // ----- Look for pre-extract callback
1688 if ((isset($p_params[AK_ZIP_PARAM_PRE_EXTRACT])) && ($p_params[AK_ZIP_PARAM_PRE_EXTRACT] != '')) {
1689 // ----- Generate a local information
1690 $v_local_header = array();
1691 $this->_convertHeader2FileInfo($p_entry, $v_local_header);
1692 // ----- Call the callback
1693 // Here I do not use call_user_func() because I need to send a reference to the
1694 // header.
1695 eval('$v_result = '.$p_params[AK_ZIP_PARAM_PRE_EXTRACT].'(AK_ZIP_PARAM_PRE_EXTRACT, $v_local_header);');
1696 if ($v_result == 0) {
1697 // ----- Change the file status
1698 $p_entry['status'] = "skipped";
1699 $v_result = 1;
1701 // ----- Update the informations
1702 // Only some fields can be modified
1703 $p_entry['filename'] = $v_local_header['filename'];
1705 // ----- Trace
1706 // ----- Look if extraction should be done
1707 if ($p_entry['status'] == 'ok') {
1708 // ----- Look for specific actions while the file exist
1709 if (file_exists($p_entry['filename'])) {
1710 // ----- Look if file is a directory
1711 if (is_dir($p_entry['filename'])) {
1712 // ----- Change the file status
1713 $p_entry['status'] = "already_a_directory";
1714 // ----- Return
1715 //return $v_result;
1718 // ----- Look if file is write protected
1719 else if (!is_writeable($p_entry['filename'])) {
1720 // ----- Change the file status
1721 $p_entry['status'] = "write_protected";
1722 // ----- Return
1723 //return $v_result;
1726 // ----- Look if the extracted file is older
1727 else if (filemtime($p_entry['filename']) > $p_entry['mtime']) {
1728 // ----- Change the file status
1729 $p_entry['status'] = "newer_exist";
1730 // ----- Return
1731 //return $v_result;
1735 // ----- Check the directory availability and create it if necessary
1736 else {
1737 if ((($p_entry['external']&0x00000010) == 0x00000010) || (substr($p_entry['filename'], -1) == '/')) $v_dir_to_check = $p_entry['filename'];
1738 else if (!strstr($p_entry['filename'], "/")) $v_dir_to_check = "";
1739 else $v_dir_to_check = dirname($p_entry['filename']);
1740 if (($v_result = $this->_dirCheck($v_dir_to_check, (($p_entry['external']&0x00000010) == 0x00000010))) != 1) {
1741 // ----- Change the file status
1742 $p_entry['status'] = "path_creation_fail";
1743 // ----- Return
1744 //return $v_result;
1745 $v_result = 1;
1749 // ----- Look if extraction should be done
1750 if ($p_entry['status'] == 'ok') {
1751 // ----- Do the extraction (if not a folder)
1752 if (!(($p_entry['external']&0x00000010) == 0x00000010)) {
1753 // ----- Look for not compressed file
1754 if ($p_entry['compressed_size'] == $p_entry['size']) {
1755 // ----- Opening destination file
1756 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
1757 // ----- Change the file status
1758 $p_entry['status'] = "write_error";
1759 // ----- Return
1760 return $v_result;
1762 // ----- Read the file by AK_ZIP_READ_BLOCK_SIZE octets blocks
1763 $v_size = $p_entry['compressed_size'];
1764 while ($v_size != 0) {
1765 $v_read_size = ($v_size < AK_ZIP_READ_BLOCK_SIZE ? $v_size : AK_ZIP_READ_BLOCK_SIZE);
1766 $v_buffer = fread($this->_zip_fd, $v_read_size);
1767 $v_binary_data = pack('a'.$v_read_size, $v_buffer);
1768 @fwrite($v_dest_file, $v_binary_data, $v_read_size);
1769 $v_size-= $v_read_size;
1771 // ----- Closing the destination file
1772 fclose($v_dest_file);
1773 // ----- Change the file mtime
1774 touch($p_entry['filename'], $p_entry['mtime']);
1775 } else {
1776 // ----- Trace
1777 // ----- Opening destination file
1778 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
1779 // ----- Change the file status
1780 $p_entry['status'] = "write_error";
1781 return $v_result;
1783 // ----- Read the compressed file in a buffer (one shot)
1784 $v_buffer = @fread($this->_zip_fd, $p_entry['compressed_size']);
1785 // ----- Decompress the file
1786 $v_file_content = gzinflate($v_buffer);
1787 unset($v_buffer);
1788 // ----- Write the uncompressed data
1789 @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
1790 unset($v_file_content);
1791 // ----- Closing the destination file
1792 @fclose($v_dest_file);
1793 // ----- Change the file mtime
1794 touch($p_entry['filename'], $p_entry['mtime']);
1796 // ----- Look for chmod option
1797 if ((isset($p_params[AK_ZIP_PARAM_SET_CHMOD])) && ($p_params[AK_ZIP_PARAM_SET_CHMOD] != 0)) {
1798 // ----- Change the mode of the file
1799 chmod($p_entry['filename'], $p_params[AK_ZIP_PARAM_SET_CHMOD]);
1803 // ----- Look for post-extract callback
1804 if ((isset($p_params[AK_ZIP_PARAM_POST_EXTRACT])) && ($p_params[AK_ZIP_PARAM_POST_EXTRACT] != '')) {
1805 // ----- Generate a local information
1806 $v_local_header = array();
1807 $this->_convertHeader2FileInfo($p_entry, $v_local_header);
1808 // ----- Call the callback
1809 // Here I do not use call_user_func() because I need to send a reference to the
1810 // header.
1811 eval('$v_result = '.$p_params[AK_ZIP_PARAM_POST_EXTRACT].'(AK_ZIP_PARAM_POST_EXTRACT, $v_local_header);');
1813 // ----- Return
1814 return $v_result;
1816 // ---------------------------------------------------------------------------
1817 // ---------------------------------------------------------------------------
1818 // Function : _extractFileAsString()
1819 // Description :
1820 // Parameters :
1821 // Return Values :
1822 // ---------------------------------------------------------------------------
1825 * AkZip::_extractFileAsString()
1827 * { Description }
1831 function _extractFileAsString(&$p_entry, &$p_string)
1833 $v_result = 1;
1834 // ----- Read the file header
1835 $v_header = array();
1836 if (($v_result = $this->_readFileHeader($v_header)) != 1) {
1837 // ----- Return
1838 return $v_result;
1840 // ----- Check that the file header is coherent with $p_entry info
1841 // TBC
1842 // ----- Trace
1843 // ----- Do the extraction (if not a folder)
1844 if (!(($p_entry['external']&0x00000010) == 0x00000010)) {
1845 // ----- Look for not compressed file
1846 if ($p_entry['compressed_size'] == $p_entry['size']) {
1847 // ----- Trace
1848 // ----- Reading the file
1849 $p_string = fread($this->_zip_fd, $p_entry['compressed_size']);
1850 } else {
1851 // ----- Trace
1852 // ----- Reading the file
1853 $v_data = fread($this->_zip_fd, $p_entry['compressed_size']);
1854 // ----- Decompress the file
1855 $p_string = gzinflate($v_data);
1857 // ----- Trace
1859 } else {
1860 // TBC : error : can not extract a folder in a string
1863 // ----- Return
1864 return $v_result;
1866 // ---------------------------------------------------------------------------
1867 // ---------------------------------------------------------------------------
1868 // Function : _readFileHeader()
1869 // Description :
1870 // Parameters :
1871 // Return Values :
1872 // ---------------------------------------------------------------------------
1875 * AkZip::_readFileHeader()
1877 * { Description }
1881 function _readFileHeader(&$p_header)
1883 $v_result = 1;
1884 // ----- Read the 4 bytes signature
1885 $v_binary_data = @fread($this->_zip_fd, 4);
1886 $v_data = unpack('Vid', $v_binary_data);
1887 // ----- Check signature
1888 if ($v_data['id'] != 0x04034b50) {
1889 // ----- Error log
1890 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
1891 // ----- Return
1892 return AkZip::errorCode();
1894 // ----- Read the first 42 bytes of the header
1895 $v_binary_data = fread($this->_zip_fd, 26);
1896 // ----- Look for invalid block size
1897 if (strlen($v_binary_data) != 26) {
1898 $p_header['filename'] = "";
1899 $p_header['status'] = "invalid_header";
1900 // ----- Error log
1901 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
1902 // ----- Return
1903 return AkZip::errorCode();
1905 // ----- Extract the values
1906 $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
1907 // ----- Get filename
1908 $p_header['filename'] = fread($this->_zip_fd, $v_data['filename_len']);
1909 // ----- Get extra_fields
1910 if ($v_data['extra_len'] != 0) {
1911 $p_header['extra'] = fread($this->_zip_fd, $v_data['extra_len']);
1912 } else {
1913 $p_header['extra'] = '';
1915 // ----- Extract properties
1916 $p_header['compression'] = $v_data['compression'];
1917 $p_header['size'] = $v_data['size'];
1918 $p_header['compressed_size'] = $v_data['compressed_size'];
1919 $p_header['crc'] = $v_data['crc'];
1920 $p_header['flag'] = $v_data['flag'];
1921 // ----- Recuperate date in UNIX format
1922 $p_header['mdate'] = $v_data['mdate'];
1923 $p_header['mtime'] = $v_data['mtime'];
1924 if ($p_header['mdate'] && $p_header['mtime']) {
1925 // ----- Extract time
1926 $v_hour = ($p_header['mtime']&0xF800) >>11;
1927 $v_minute = ($p_header['mtime']&0x07E0) >>5;
1928 $v_seconde = ($p_header['mtime']&0x001F) *2;
1929 // ----- Extract date
1930 $v_year = (($p_header['mdate']&0xFE00) >>9) +1980;
1931 $v_month = ($p_header['mdate']&0x01E0) >>5;
1932 $v_day = $p_header['mdate']&0x001F;
1933 // ----- Get UNIX date format
1934 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
1935 } else {
1936 $p_header['mtime'] = time();
1938 // ----- Other informations
1939 // TBC
1940 //for(reset($v_data); $key = key($v_data); next($v_data)) {
1942 // ----- Set the stored filename
1943 $p_header['stored_filename'] = $p_header['filename'];
1944 // ----- Set the status field
1945 $p_header['status'] = "ok";
1946 // ----- Return
1947 return $v_result;
1949 // ---------------------------------------------------------------------------
1950 // ---------------------------------------------------------------------------
1951 // Function : _readCentralFileHeader()
1952 // Description :
1953 // Parameters :
1954 // Return Values :
1955 // ---------------------------------------------------------------------------
1958 * AkZip::_readCentralFileHeader()
1960 * { Description }
1964 function _readCentralFileHeader(&$p_header)
1966 $v_result = 1;
1967 // ----- Read the 4 bytes signature
1968 $v_binary_data = @fread($this->_zip_fd, 4);
1969 $v_data = unpack('Vid', $v_binary_data);
1970 // ----- Check signature
1971 if ($v_data['id'] != 0x02014b50) {
1972 // ----- Error log
1973 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
1974 // ----- Return
1975 return AkZip::errorCode();
1977 // ----- Read the first 42 bytes of the header
1978 $v_binary_data = fread($this->_zip_fd, 42);
1979 // ----- Look for invalid block size
1980 if (strlen($v_binary_data) != 42) {
1981 $p_header['filename'] = "";
1982 $p_header['status'] = "invalid_header";
1983 // ----- Error log
1984 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
1985 // ----- Return
1986 return AkZip::errorCode();
1988 // ----- Extract the values
1989 $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);
1990 // ----- Get filename
1991 if ($p_header['filename_len'] != 0) $p_header['filename'] = fread($this->_zip_fd, $p_header['filename_len']);
1992 else $p_header['filename'] = '';
1993 // ----- Get extra
1994 if ($p_header['extra_len'] != 0) $p_header['extra'] = fread($this->_zip_fd, $p_header['extra_len']);
1995 else $p_header['extra'] = '';
1996 // ----- Get comment
1997 if ($p_header['comment_len'] != 0) $p_header['comment'] = fread($this->_zip_fd, $p_header['comment_len']);
1998 else $p_header['comment'] = '';
1999 // ----- Extract properties
2000 // ----- Recuperate date in UNIX format
2001 if ($p_header['mdate'] && $p_header['mtime']) {
2002 // ----- Extract time
2003 $v_hour = ($p_header['mtime']&0xF800) >>11;
2004 $v_minute = ($p_header['mtime']&0x07E0) >>5;
2005 $v_seconde = ($p_header['mtime']&0x001F) *2;
2006 // ----- Extract date
2007 $v_year = (($p_header['mdate']&0xFE00) >>9) +1980;
2008 $v_month = ($p_header['mdate']&0x01E0) >>5;
2009 $v_day = $p_header['mdate']&0x001F;
2010 // ----- Get UNIX date format
2011 $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
2012 } else {
2013 $p_header['mtime'] = time();
2015 // ----- Set the stored filename
2016 $p_header['stored_filename'] = $p_header['filename'];
2017 // ----- Set default status to ok
2018 $p_header['status'] = 'ok';
2019 // ----- Look if it is a directory
2020 if (substr($p_header['filename'], -1) == '/') {
2021 $p_header['external'] = 0x41FF0010;
2023 // ----- Return
2024 return $v_result;
2026 // ---------------------------------------------------------------------------
2027 // ---------------------------------------------------------------------------
2028 // Function : _readEndCentralDir()
2029 // Description :
2030 // Parameters :
2031 // Return Values :
2032 // ---------------------------------------------------------------------------
2035 * AkZip::_readEndCentralDir()
2037 * { Description }
2041 function _readEndCentralDir(&$p_central_dir)
2043 $v_result = 1;
2044 // ----- Go to the end of the zip file
2045 $v_size = filesize($this->_zipname);
2046 @fseek($this->_zip_fd, $v_size);
2047 if (@ftell($this->_zip_fd) != $v_size) {
2048 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->_zipname.'\'');
2049 return AkZip::errorCode();
2051 // ----- First try : look if this is an archive with no commentaries
2052 // (most of the time)
2053 // in this case the end of central dir is at 22 bytes of the file end
2054 $v_found = 0;
2055 if ($v_size > 26) {
2056 @fseek($this->_zip_fd, $v_size-22);
2057 if (($v_pos = @ftell($this->_zip_fd)) != ($v_size-22)) {
2058 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->_zipname.'\'');
2059 return AkZip::errorCode();
2061 // ----- Read for bytes
2062 $v_binary_data = @fread($this->_zip_fd, 4);
2063 $v_data = unpack('Vid', $v_binary_data);
2064 // ----- Check signature
2065 if ($v_data['id'] == 0x06054b50) {
2066 $v_found = 1;
2068 $v_pos = ftell($this->_zip_fd);
2070 // ----- Go back to the maximum possible size of the Central Dir End Record
2071 if (!$v_found) {
2072 $v_maximum_size = 65557; // 0xFFFF + 22;
2073 if ($v_maximum_size > $v_size) $v_maximum_size = $v_size;
2074 @fseek($this->_zip_fd, $v_size-$v_maximum_size);
2075 if (@ftell($this->_zip_fd) != ($v_size-$v_maximum_size)) {
2076 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->_zipname.'\'');
2077 return AkZip::errorCode();
2079 // ----- Read byte per byte in order to find the signature
2080 $v_pos = ftell($this->_zip_fd);
2081 $v_bytes = 0x00000000;
2082 while ($v_pos < $v_size) {
2083 // ----- Read a byte
2084 $v_byte = @fread($this->_zip_fd, 1);
2085 // ----- Add the byte
2086 $v_bytes = ($v_bytes<<8) |Ord($v_byte);
2087 // ----- Compare the bytes
2088 if ($v_bytes == 0x504b0506) {
2089 $v_pos++;
2090 break;
2092 $v_pos++;
2094 // ----- Look if not found end of central dir
2095 if ($v_pos == $v_size) {
2096 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
2097 return AkZip::errorCode();
2100 // ----- Read the first 18 bytes of the header
2101 $v_binary_data = fread($this->_zip_fd, 18);
2102 // ----- Look for invalid block size
2103 if (strlen($v_binary_data) != 18) {
2104 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
2105 return AkZip::errorCode();
2107 // ----- Extract the values
2108 $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
2109 // ----- Check the global size
2110 if (($v_pos+$v_data['comment_size']+18) != $v_size) {
2111 $this->_errorLog(AK_ZIP_ERR_BAD_FORMAT, "Fail to find the right signature");
2112 return AkZip::errorCode();
2114 // ----- Get comment
2115 if ($v_data['comment_size'] != 0) $p_central_dir['comment'] = fread($this->_zip_fd, $v_data['comment_size']);
2116 else $p_central_dir['comment'] = '';
2117 $p_central_dir['entries'] = $v_data['entries'];
2118 $p_central_dir['disk_entries'] = $v_data['disk_entries'];
2119 $p_central_dir['offset'] = $v_data['offset'];
2120 $p_central_dir['size'] = $v_data['size'];
2121 $p_central_dir['disk'] = $v_data['disk'];
2122 $p_central_dir['disk_start'] = $v_data['disk_start'];
2123 // ----- Return
2124 return $v_result;
2126 // ---------------------------------------------------------------------------
2127 // ---------------------------------------------------------------------------
2128 // Function : _deleteByRule()
2129 // Description :
2130 // Parameters :
2131 // Return Values :
2132 // ---------------------------------------------------------------------------
2135 * AkZip::_deleteByRule()
2137 * { Description }
2141 function _deleteByRule(&$p_result_list, &$p_params)
2143 $v_result = 1;
2144 $v_list_detail = array();
2145 // ----- Open the zip file
2146 if (($v_result = $this->_openFd('rb')) != 1) {
2147 // ----- Return
2148 return $v_result;
2150 // ----- Read the central directory informations
2151 $v_central_dir = array();
2152 if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
2153 $this->_closeFd();
2154 return $v_result;
2156 // ----- Go to beginning of File
2157 @rewind($this->_zip_fd);
2158 // ----- Scan all the files
2159 // ----- Start at beginning of Central Dir
2160 $v_pos_entry = $v_central_dir['offset'];
2161 @rewind($this->_zip_fd);
2162 if (@fseek($this->_zip_fd, $v_pos_entry)) {
2163 // ----- Clean
2164 $this->_closeFd();
2165 $this->_errorLog(AK_ZIP_ERR_INVALID_AK_ZIP, 'Invalid archive size');
2166 return AkZip::errorCode();
2168 // ----- Read each entry
2169 $v_header_list = array();
2170 $j_start = 0;
2171 for ($i = 0, $v_nb_extracted = 0 ; $i < $v_central_dir['entries'] ; $i++) {
2172 // ----- Read the file header
2173 $v_header_list[$v_nb_extracted] = array();
2174 $v_result = $this->_readCentralFileHeader($v_header_list[$v_nb_extracted]);
2175 if ($v_result != 1) {
2176 // ----- Clean
2177 $this->_closeFd();
2178 return $v_result;
2180 // ----- Store the index
2181 $v_header_list[$v_nb_extracted]['index'] = $i;
2182 // ----- Look for the specific extract rules
2183 $v_found = false;
2184 // ----- Look for extract by name rule
2185 if ((isset($p_params[AK_ZIP_PARAM_BY_NAME])) && ($p_params[AK_ZIP_PARAM_BY_NAME] != 0)) {
2186 // ----- Look if the filename is in the list
2187 for ($j = 0 ; ($j < sizeof($p_params[AK_ZIP_PARAM_BY_NAME])) && (!$v_found) ; $j++) {
2188 // ----- Look for a directory
2189 if (substr($p_params[AK_ZIP_PARAM_BY_NAME][$j], -1) == "/") {
2190 // ----- Look if the directory is in the filename path
2191 if ((strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_params[AK_ZIP_PARAM_BY_NAME][$j])) && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_params[AK_ZIP_PARAM_BY_NAME][$j])) == $p_params[AK_ZIP_PARAM_BY_NAME][$j])) {
2192 $v_found = true;
2193 } elseif ((($v_header_list[$v_nb_extracted]['external']&0x00000010) == 0x00000010) /* Indicates a folder */ && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_params[AK_ZIP_PARAM_BY_NAME][$j])) {
2194 $v_found = true;
2197 // ----- Look for a filename
2198 elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_params[AK_ZIP_PARAM_BY_NAME][$j]) {
2199 $v_found = true;
2203 // ----- Look for extract by ereg rule
2204 else if ((isset($p_params[AK_ZIP_PARAM_BY_EREG])) && ($p_params[AK_ZIP_PARAM_BY_EREG] != "")) {
2205 if (ereg($p_params[AK_ZIP_PARAM_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
2206 $v_found = true;
2209 // ----- Look for extract by preg rule
2210 else if ((isset($p_params[AK_ZIP_PARAM_BY_PREG])) && ($p_params[AK_ZIP_PARAM_BY_PREG] != "")) {
2211 if (preg_match($p_params[AK_ZIP_PARAM_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
2212 $v_found = true;
2215 // ----- Look for extract by index rule
2216 else if ((isset($p_params[AK_ZIP_PARAM_BY_INDEX])) && ($p_params[AK_ZIP_PARAM_BY_INDEX] != 0)) {
2217 // ----- Look if the index is in the list
2218 for ($j = $j_start ; ($j < sizeof($p_params[AK_ZIP_PARAM_BY_INDEX])) && (!$v_found) ; $j++) {
2219 if (($i >= $p_params[AK_ZIP_PARAM_BY_INDEX][$j]['start']) && ($i <= $p_params[AK_ZIP_PARAM_BY_INDEX][$j]['end'])) {
2220 $v_found = true;
2222 if ($i >= $p_params[AK_ZIP_PARAM_BY_INDEX][$j]['end']) {
2223 $j_start = $j+1;
2225 if ($p_params[AK_ZIP_PARAM_BY_INDEX][$j]['start'] > $i) {
2226 break;
2230 // ----- Look for deletion
2231 if ($v_found) {
2232 unset($v_header_list[$v_nb_extracted]);
2233 } else {
2234 $v_nb_extracted++;
2237 // ----- Look if something need to be deleted
2238 if ($v_nb_extracted > 0) {
2239 // ----- Creates a temporay file
2240 $v_zip_temp_name = AK_ZIP_TEMPORARY_DIR.uniqid('archive_zip-') .'.tmp';
2241 // ----- Creates a temporary zip archive
2242 $v_temp_zip = new AkZip($v_zip_temp_name);
2243 // ----- Open the temporary zip file in write mode
2244 if (($v_result = $v_temp_zip->_openFd('wb')) != 1) {
2245 $this->_closeFd();
2246 // ----- Return
2247 return $v_result;
2249 // ----- Look which file need to be kept
2250 for ($i = 0 ; $i < sizeof($v_header_list) ; $i++) {
2251 // ----- Calculate the position of the header
2252 @rewind($this->_zip_fd);
2253 if (@fseek($this->_zip_fd, $v_header_list[$i]['offset'])) {
2254 // ----- Clean
2255 $this->_closeFd();
2256 $v_temp_zip->_closeFd();
2257 @unlink($v_zip_temp_name);
2258 $this->_errorLog(AK_ZIP_ERR_INVALID_AK_ZIP, 'Invalid archive size');
2259 return AkZip::errorCode();
2261 // ----- Read the file header
2262 if (($v_result = $this->_readFileHeader($v_header_list[$i])) != 1) {
2263 // ----- Clean
2264 $this->_closeFd();
2265 $v_temp_zip->_closeFd();
2266 @unlink($v_zip_temp_name);
2267 return $v_result;
2269 // ----- Write the file header
2270 $v_result = $v_temp_zip->_writeFileHeader($v_header_list[$i]);
2271 if ($v_result != 1) {
2272 // ----- Clean
2273 $this->_closeFd();
2274 $v_temp_zip->_closeFd();
2275 @unlink($v_zip_temp_name);
2276 return $v_result;
2278 // ----- Read/write the data block
2279 $v_result = $this->_tool_CopyBlock($this->_zip_fd, $v_temp_zip->_zip_fd, $v_header_list[$i]['compressed_size']);
2280 if ($v_result != 1) {
2281 // ----- Clean
2282 $this->_closeFd();
2283 $v_temp_zip->_closeFd();
2284 @unlink($v_zip_temp_name);
2285 return $v_result;
2288 // ----- Store the offset of the central dir
2289 $v_offset = @ftell($v_temp_zip->_zip_fd);
2290 // ----- Re-Create the Central Dir files header
2291 for ($i = 0 ; $i < sizeof($v_header_list) ; $i++) {
2292 // ----- Create the file header
2293 $v_result = $v_temp_zip->_writeCentralFileHeader($v_header_list[$i]);
2294 if ($v_result != 1) {
2295 // ----- Clean
2296 $v_temp_zip->_closeFd();
2297 $this->_closeFd();
2298 @unlink($v_zip_temp_name);
2299 return $v_result;
2301 // ----- Transform the header to a 'usable' info
2302 $v_temp_zip->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2304 // ----- Zip file comment
2305 $v_comment = '';
2306 // ----- Calculate the size of the central header
2307 $v_size = @ftell($v_temp_zip->_zip_fd) -$v_offset;
2308 // ----- Create the central dir footer
2309 $v_result = $v_temp_zip->_writeCentralHeader(sizeof($v_header_list) , $v_size, $v_offset, $v_comment);
2310 if ($v_result != 1) {
2311 // ----- Clean
2312 unset($v_header_list);
2313 $v_temp_zip->_closeFd();
2314 $this->_closeFd();
2315 @unlink($v_zip_temp_name);
2316 return $v_result;
2318 // ----- Close
2319 $v_temp_zip->_closeFd();
2320 $this->_closeFd();
2321 // ----- Delete the zip file
2322 // TBC : I should test the result ...
2323 @unlink($this->_zipname);
2324 // ----- Rename the temporary file
2325 // TBC : I should test the result ...
2326 //@rename($v_zip_temp_name, $this->_zipname);
2327 $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
2328 // ----- Destroy the temporary archive
2329 unset($v_temp_zip);
2331 // ----- Return
2332 return $v_result;
2334 // ---------------------------------------------------------------------------
2335 // ---------------------------------------------------------------------------
2336 // Function : _dirCheck()
2337 // Description :
2338 // Check if a directory exists, if not it creates it and all the parents directory
2339 // which may be useful.
2340 // Parameters :
2341 // $p_dir : Directory path to check.
2342 // Return Values :
2343 // 1 : OK
2344 // -1 : Unable to create directory
2345 // ---------------------------------------------------------------------------
2348 * AkZip::_dirCheck()
2350 * { Description }
2352 * @param [type] $p_is_dir
2355 function _dirCheck($p_dir, $p_is_dir = false)
2357 $v_result = 1;
2358 // ----- Remove the final '/'
2359 if (($p_is_dir) && (substr($p_dir, -1) == '/')) {
2360 $p_dir = substr($p_dir, 0, strlen($p_dir) -1);
2362 // ----- Check the directory availability
2363 if ((is_dir($p_dir)) || ($p_dir == "")) {
2364 return 1;
2366 // ----- Extract parent directory
2367 $p_parent_dir = dirname($p_dir);
2368 // ----- Just a check
2369 if ($p_parent_dir != $p_dir) {
2370 // ----- Look for parent directory
2371 if ($p_parent_dir != "") {
2372 if (($v_result = $this->_dirCheck($p_parent_dir)) != 1) {
2373 return $v_result;
2377 // ----- Create the directory
2378 if (!@mkdir($p_dir, 0777)) {
2379 $this->_errorLog(AK_ZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
2380 return AkZip::errorCode();
2382 // ----- Return
2383 return $v_result;
2385 // ---------------------------------------------------------------------------
2386 // ---------------------------------------------------------------------------
2387 // Function : _merge()
2388 // Description :
2389 // If $p_archive_to_add does not exist, the function exit with a success result.
2390 // Parameters :
2391 // Return Values :
2392 // ---------------------------------------------------------------------------
2395 * AkZip::_merge()
2397 * { Description }
2401 function _merge(&$p_archive_to_add)
2403 $v_result = 1;
2404 // ----- Look if the archive_to_add exists
2405 if (!is_file($p_archive_to_add->_zipname)) {
2406 // ----- Nothing to merge, so merge is a success
2407 return 1;
2409 // ----- Look if the archive exists
2410 if (!is_file($this->_zipname)) {
2411 // ----- Do a duplicate
2412 $v_result = $this->_duplicate($p_archive_to_add->_zipname);
2413 return $v_result;
2415 // ----- Open the zip file
2416 if (($v_result = $this->_openFd('rb')) != 1) {
2417 return $v_result;
2419 // ----- Read the central directory informations
2420 $v_central_dir = array();
2421 if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
2422 $this->_closeFd();
2423 return $v_result;
2425 // ----- Go to beginning of File
2426 @rewind($this->_zip_fd);
2427 // ----- Open the archive_to_add file
2428 if (($v_result = $p_archive_to_add->_openFd('rb')) != 1) {
2429 $this->_closeFd();
2430 return $v_result;
2432 // ----- Read the central directory informations
2433 $v_central_dir_to_add = array();
2434 $v_result = $p_archive_to_add->_readEndCentralDir($v_central_dir_to_add);
2435 if ($v_result != 1) {
2436 $this->_closeFd();
2437 $p_archive_to_add->_closeFd();
2438 return $v_result;
2440 // ----- Go to beginning of File
2441 @rewind($p_archive_to_add->_zip_fd);
2442 // ----- Creates a temporay file
2443 $v_zip_temp_name = AK_ZIP_TEMPORARY_DIR.uniqid('archive_zip-') .'.tmp';
2444 // ----- Open the temporary file in write mode
2445 if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
2446 $this->_closeFd();
2447 $p_archive_to_add->_closeFd();
2448 $this->_errorLog(AK_ZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
2449 return AkZip::errorCode();
2451 // ----- Copy the files from the archive to the temporary file
2452 // TBC : Here I should better append the file and go back to erase the
2453 // central dir
2454 $v_size = $v_central_dir['offset'];
2455 while ($v_size != 0) {
2456 $v_read_size = ($v_size < AK_ZIP_READ_BLOCK_SIZE ? $v_size : AK_ZIP_READ_BLOCK_SIZE);
2457 $v_buffer = fread($this->_zip_fd, $v_read_size);
2458 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
2459 $v_size-= $v_read_size;
2461 // ----- Copy the files from the archive_to_add into the temporary file
2462 $v_size = $v_central_dir_to_add['offset'];
2463 while ($v_size != 0) {
2464 $v_read_size = ($v_size < AK_ZIP_READ_BLOCK_SIZE ? $v_size : AK_ZIP_READ_BLOCK_SIZE);
2465 $v_buffer = fread($p_archive_to_add->_zip_fd, $v_read_size);
2466 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
2467 $v_size-= $v_read_size;
2469 // ----- Store the offset of the central dir
2470 $v_offset = @ftell($v_zip_temp_fd);
2471 // ----- Copy the block of file headers from the old archive
2472 $v_size = $v_central_dir['size'];
2473 while ($v_size != 0) {
2474 $v_read_size = ($v_size < AK_ZIP_READ_BLOCK_SIZE ? $v_size : AK_ZIP_READ_BLOCK_SIZE);
2475 $v_buffer = @fread($this->_zip_fd, $v_read_size);
2476 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
2477 $v_size-= $v_read_size;
2479 // ----- Copy the block of file headers from the archive_to_add
2480 $v_size = $v_central_dir_to_add['size'];
2481 while ($v_size != 0) {
2482 $v_read_size = ($v_size < AK_ZIP_READ_BLOCK_SIZE ? $v_size : AK_ZIP_READ_BLOCK_SIZE);
2483 $v_buffer = @fread($p_archive_to_add->_zip_fd, $v_read_size);
2484 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
2485 $v_size-= $v_read_size;
2487 // ----- Zip file comment
2488 // TBC : I should merge the two comments
2489 $v_comment = '';
2490 // ----- Calculate the size of the (new) central header
2491 $v_size = @ftell($v_zip_temp_fd) -$v_offset;
2492 // ----- Swap the file descriptor
2493 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
2494 // the following methods on the temporary fil and not the real archive fd
2495 $v_swap = $this->_zip_fd;
2496 $this->_zip_fd = $v_zip_temp_fd;
2497 $v_zip_temp_fd = $v_swap;
2498 // ----- Create the central dir footer
2499 if (($v_result = $this->_writeCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1) {
2500 $this->_closeFd();
2501 $p_archive_to_add->_closeFd();
2502 @fclose($v_zip_temp_fd);
2503 $this->_zip_fd = null;
2504 // ----- Reset the file list
2505 unset($v_header_list);
2506 // ----- Return
2507 return $v_result;
2509 // ----- Swap back the file descriptor
2510 $v_swap = $this->_zip_fd;
2511 $this->_zip_fd = $v_zip_temp_fd;
2512 $v_zip_temp_fd = $v_swap;
2513 // ----- Close
2514 $this->_closeFd();
2515 $p_archive_to_add->_closeFd();
2516 // ----- Close the temporary file
2517 @fclose($v_zip_temp_fd);
2518 // ----- Delete the zip file
2519 // TBC : I should test the result ...
2520 @unlink($this->_zipname);
2521 // ----- Rename the temporary file
2522 // TBC : I should test the result ...
2523 //@rename($v_zip_temp_name, $this->_zipname);
2524 $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
2525 // ----- Return
2526 return $v_result;
2528 // ---------------------------------------------------------------------------
2529 // ---------------------------------------------------------------------------
2530 // Function : _duplicate()
2531 // Description :
2532 // Parameters :
2533 // Return Values :
2534 // ---------------------------------------------------------------------------
2537 * AkZip::_duplicate()
2539 * { Description }
2543 function _duplicate($p_archive_filename)
2545 $v_result = 1;
2546 // ----- Look if the $p_archive_filename exists
2547 if (!is_file($p_archive_filename)) {
2548 // ----- Nothing to duplicate, so duplicate is a success.
2549 $v_result = 1;
2550 // ----- Return
2551 return $v_result;
2553 // ----- Open the zip file
2554 if (($v_result = $this->_openFd('wb')) != 1) {
2555 // ----- Return
2556 return $v_result;
2558 // ----- Open the temporary file in write mode
2559 if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) {
2560 $this->_closeFd();
2561 $this->_errorLog(AK_ZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
2562 return AkZip::errorCode();
2564 // ----- Copy the files from the archive to the temporary file
2565 // TBC : Here I should better append the file and go back to erase the
2566 // central dir
2567 $v_size = filesize($p_archive_filename);
2568 while ($v_size != 0) {
2569 $v_read_size = ($v_size < AK_ZIP_READ_BLOCK_SIZE ? $v_size : AK_ZIP_READ_BLOCK_SIZE);
2570 $v_buffer = fread($v_zip_temp_fd, $v_read_size);
2571 @fwrite($this->_zip_fd, $v_buffer, $v_read_size);
2572 $v_size-= $v_read_size;
2574 // ----- Close
2575 $this->_closeFd();
2576 // ----- Close the temporary file
2577 @fclose($v_zip_temp_fd);
2578 return $v_result;
2580 // ---------------------------------------------------------------------------
2583 * AkZip::_check_parameters()
2585 * { Description }
2587 * @param integer $p_error_code
2588 * @param string $p_error_string
2591 function _check_parameters(&$p_params, $p_default)
2593 // ----- Check that param is an array
2594 if (!is_array($p_params)) {
2595 $this->_errorLog(AK_ZIP_ERR_INVALID_PARAMETER, 'Unsupported parameter, waiting for an array');
2596 return AkZip::errorCode();
2598 // ----- Check that all the params are valid
2599 for (reset($p_params) ; list($v_key, $v_value) = each($p_params) ;) {
2600 if (!isset($p_default[$v_key])) {
2601 $this->_errorLog(AK_ZIP_ERR_INVALID_PARAMETER, 'Unsupported parameter with key \''.$v_key.'\'');
2602 return AkZip::errorCode();
2605 // ----- Set the default values
2606 for (reset($p_default) ; list($v_key, $v_value) = each($p_default) ;) {
2607 if (!isset($p_params[$v_key])) {
2608 $p_params[$v_key] = $p_default[$v_key];
2611 // ----- Check specific parameters
2612 $v_callback_list = array(
2613 'callback_pre_add',
2614 'callback_post_add',
2615 'callback_pre_extract',
2616 'callback_post_extract'
2618 for ($i = 0 ; $i < sizeof($v_callback_list) ; $i++) {
2619 $v_key = $v_callback_list[$i];
2620 if ((isset($p_params[$v_key])) && ($p_params[$v_key] != '')) {
2621 if (!function_exists($p_params[$v_key])) {
2622 $this->_errorLog(AK_ZIP_ERR_INVALID_PARAM_VALUE, "Callback '".$p_params[$v_key]."()' is not an existing function for "."parameter '".$v_key."'");
2623 return AkZip::errorCode();
2627 return (1);
2629 // ---------------------------------------------------------------------------
2630 // ---------------------------------------------------------------------------
2631 // Function : _errorLog()
2632 // Description :
2633 // Parameters :
2634 // ---------------------------------------------------------------------------
2637 * AkZip::_errorLog()
2639 * { Description }
2641 * @param integer $p_error_code
2642 * @param string $p_error_string
2645 function _errorLog($p_error_code = 0, $p_error_string = '')
2647 $this->_error_code = $p_error_code;
2648 $this->_error_string = $p_error_string;
2650 // ---------------------------------------------------------------------------
2651 // ---------------------------------------------------------------------------
2652 // Function : _errorReset()
2653 // Description :
2654 // Parameters :
2655 // ---------------------------------------------------------------------------
2658 * AkZip::_errorReset()
2660 * { Description }
2664 function _errorReset()
2666 $this->_error_code = 1;
2667 $this->_error_string = '';
2669 // ---------------------------------------------------------------------------
2670 // ---------------------------------------------------------------------------
2671 // Function : $this->_tool_PathReduction()
2672 // Description :
2673 // Parameters :
2674 // Return Values :
2675 // ---------------------------------------------------------------------------
2678 * _tool_PathReduction()
2680 * { Description }
2684 function _tool_PathReduction($p_dir)
2686 $v_result = "";
2687 // ----- Look for not empty path
2688 if ($p_dir != "") {
2689 // ----- Explode path by directory names
2690 $v_list = explode("/", $p_dir);
2691 // ----- Study directories from last to first
2692 for ($i = sizeof($v_list) -1 ; $i >= 0 ; $i--) {
2693 // ----- Look for current path
2694 if ($v_list[$i] == ".") {
2695 // ----- Ignore this directory
2696 // Should be the first $i=0, but no check is done
2698 } else if ($v_list[$i] == "..") {
2699 // ----- Ignore it and ignore the $i-1
2700 $i--;
2701 } else if (($v_list[$i] == "") && ($i != (sizeof($v_list) -1)) && ($i != 0)) {
2702 // ----- Ignore only the double '//' in path,
2703 // but not the first and last '/'
2705 } else {
2706 $v_result = $v_list[$i].($i != (sizeof($v_list) -1) ? "/".$v_result : "");
2710 // ----- Return
2711 return $v_result;
2713 // ---------------------------------------------------------------------------
2714 // ---------------------------------------------------------------------------
2715 // Function : $this->_tool_PathInclusion()
2716 // Description :
2717 // This function indicates if the path $p_path is under the $p_dir tree. Or,
2718 // said in an other way, if the file or sub-dir $p_path is inside the dir
2719 // $p_dir.
2720 // The function indicates also if the path is exactly the same as the dir.
2721 // This function supports path with duplicated '/' like '//', but does not
2722 // support '.' or '..' statements.
2723 // Parameters :
2724 // Return Values :
2725 // 0 if $p_path is not inside directory $p_dir
2726 // 1 if $p_path is inside directory $p_dir
2727 // 2 if $p_path is exactly the same as $p_dir
2728 // ---------------------------------------------------------------------------
2731 * _tool_PathInclusion()
2733 * { Description }
2737 function _tool_PathInclusion($p_dir, $p_path)
2739 $v_result = 1;
2740 // ----- Explode dir and path by directory separator
2741 $v_list_dir = explode("/", $p_dir);
2742 $v_list_dir_size = sizeof($v_list_dir);
2743 $v_list_path = explode("/", $p_path);
2744 $v_list_path_size = sizeof($v_list_path);
2745 // ----- Study directories paths
2746 $i = 0;
2747 $j = 0;
2748 while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
2749 // ----- Look for empty dir (path reduction)
2750 if ($v_list_dir[$i] == '') {
2751 $i++;
2752 continue;
2754 if ($v_list_path[$j] == '') {
2755 $j++;
2756 continue;
2758 // ----- Compare the items
2759 if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ($v_list_path[$j] != '')) {
2760 $v_result = 0;
2762 // ----- Next items
2763 $i++;
2764 $j++;
2766 // ----- Look if everything seems to be the same
2767 if ($v_result) {
2768 // ----- Skip all the empty items
2769 while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
2770 while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
2771 if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
2772 // ----- There are exactly the same
2773 $v_result = 2;
2774 } else if ($i < $v_list_dir_size) {
2775 // ----- The path is shorter than the dir
2776 $v_result = 0;
2779 // ----- Return
2780 return $v_result;
2782 // ---------------------------------------------------------------------------
2783 // ---------------------------------------------------------------------------
2784 // Function : $this->_tool_CopyBlock()
2785 // Description :
2786 // Parameters :
2787 // $p_mode : read/write compression mode
2788 // 0 : src & dest normal
2789 // 1 : src gzip, dest normal
2790 // 2 : src normal, dest gzip
2791 // 3 : src & dest gzip
2792 // Return Values :
2793 // ---------------------------------------------------------------------------
2796 * _tool_CopyBlock()
2798 * { Description }
2800 * @param integer $p_mode
2803 function _tool_CopyBlock($p_src, $p_dest, $p_size, $p_mode = 0)
2805 $v_result = 1;
2806 if ($p_mode == 0) {
2807 while ($p_size != 0) {
2808 $v_read_size = ($p_size < AK_ZIP_READ_BLOCK_SIZE ? $p_size : AK_ZIP_READ_BLOCK_SIZE);
2809 $v_buffer = @fread($p_src, $v_read_size);
2810 @fwrite($p_dest, $v_buffer, $v_read_size);
2811 $p_size-= $v_read_size;
2813 } else if ($p_mode == 1) {
2814 while ($p_size != 0) {
2815 $v_read_size = ($p_size < AK_ZIP_READ_BLOCK_SIZE ? $p_size : AK_ZIP_READ_BLOCK_SIZE);
2816 $v_buffer = @gzread($p_src, $v_read_size);
2817 @fwrite($p_dest, $v_buffer, $v_read_size);
2818 $p_size-= $v_read_size;
2820 } else if ($p_mode == 2) {
2821 while ($p_size != 0) {
2822 $v_read_size = ($p_size < AK_ZIP_READ_BLOCK_SIZE ? $p_size : AK_ZIP_READ_BLOCK_SIZE);
2823 $v_buffer = @fread($p_src, $v_read_size);
2824 @gzwrite($p_dest, $v_buffer, $v_read_size);
2825 $p_size-= $v_read_size;
2827 } else if ($p_mode == 3) {
2828 while ($p_size != 0) {
2829 $v_read_size = ($p_size < AK_ZIP_READ_BLOCK_SIZE ? $p_size : AK_ZIP_READ_BLOCK_SIZE);
2830 $v_buffer = @gzread($p_src, $v_read_size);
2831 @gzwrite($p_dest, $v_buffer, $v_read_size);
2832 $p_size-= $v_read_size;
2835 // ----- Return
2836 return $v_result;
2838 // ---------------------------------------------------------------------------
2839 // ---------------------------------------------------------------------------
2840 // Function : $this->_tool_Rename()
2841 // Description :
2842 // This function tries to do a simple rename() function. If it fails, it
2843 // tries to copy the $p_src file in a new $p_dest file and then unlink the
2844 // first one.
2845 // Parameters :
2846 // $p_src : Old filename
2847 // $p_dest : New filename
2848 // Return Values :
2849 // 1 on success, 0 on failure.
2850 // ---------------------------------------------------------------------------
2853 * _tool_Rename()
2855 * { Description }
2859 function _tool_Rename($p_src, $p_dest)
2861 $v_result = 1;
2862 // ----- Try to rename the files
2863 if (!@rename($p_src, $p_dest)) {
2864 // ----- Try to copy & unlink the src
2865 if (!@copy($p_src, $p_dest)) {
2866 $v_result = 0;
2867 } else if (!@unlink($p_src)) {
2868 $v_result = 0;
2871 // ----- Return
2872 return $v_result;
2874 // ---------------------------------------------------------------------------
2875 // ---------------------------------------------------------------------------
2876 // Function : $this->_tool_TranslateWinPath()
2877 // Description :
2878 // Translate windows path by replacing '\' by '/' and optionally removing
2879 // drive letter.
2880 // Parameters :
2881 // $p_path : path to translate.
2882 // $p_remove_disk_letter : true | false
2883 // Return Values :
2884 // The path translated.
2885 // ---------------------------------------------------------------------------
2888 * _tool_TranslateWinPath()
2890 * { Description }
2892 * @param [type] $p_remove_disk_letter
2895 function _tool_TranslateWinPath($p_path, $p_remove_disk_letter = true)
2897 if (stristr(php_uname() , 'windows')) {
2898 // ----- Look for potential disk letter
2899 if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
2900 $p_path = substr($p_path, $v_position+1);
2902 // ----- Change potential windows directory separator
2903 if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
2904 $p_path = strtr($p_path, '\\', '/');
2907 return $p_path;
2909 // ---------------------------------------------------------------------------
2912 // End of class