home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Archive / Zip.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  117.0 KB  |  3,607 lines

  1. <?php
  2. /* vim: set ts=4 sw=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This library is free software; you can redistribute it and/or        |
  9. // | modify it under the terms of the GNU Lesser General Public           |
  10. // | License as published by the Free Software Foundation; either         |
  11. // | version 2.1 of the License, or (at your option) any later version.   |
  12. // |                                                                      |
  13. // | This library is distributed in the hope that it will be useful,      |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
  16. // | Lesser General Public License for more details.                      |
  17. // |                                                                      |
  18. // | You should have received a copy of the GNU Lesser General Public     |
  19. // | License along with this library; if not, write to the Free Software  |
  20. // | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,           |
  21. // | MA  02110-1301  USA                                                  |
  22. // +----------------------------------------------------------------------+
  23. // | Author: Vincent Blavet <vincent@phpconcept.net>                      |
  24. // +----------------------------------------------------------------------+
  25. //
  26. // $Id: Zip.php,v 1.2 2005/11/21 06:51:57 vblavet Exp $
  27.  
  28.   require_once 'PEAR.php';
  29.  
  30.   // ----- Constants
  31.   define( 'ARCHIVE_ZIP_READ_BLOCK_SIZE', 2048 );
  32.  
  33.   // ----- File list separator
  34.   define( 'ARCHIVE_ZIP_SEPARATOR', ',' );
  35.  
  36.   // ----- Optional static temporary directory
  37.   //       By default temporary files are generated in the script current
  38.   //       path.
  39.   //       If defined :
  40.   //       - MUST BE terminated by a '/'.
  41.   //       - MUST be a valid, already created directory
  42.   //       Samples :
  43.   // define( 'ARCHIVE_ZIP_TEMPORARY_DIR', '/temp/' );
  44.   // define( 'ARCHIVE_ZIP_TEMPORARY_DIR', 'C:/Temp/' );
  45.   define( 'ARCHIVE_ZIP_TEMPORARY_DIR', '' );
  46.  
  47.   // ----- Error codes
  48.   define( 'ARCHIVE_ZIP_ERR_NO_ERROR', 0 );
  49.   define( 'ARCHIVE_ZIP_ERR_WRITE_OPEN_FAIL', -1 );
  50.   define( 'ARCHIVE_ZIP_ERR_READ_OPEN_FAIL', -2 );
  51.   define( 'ARCHIVE_ZIP_ERR_INVALID_PARAMETER', -3 );
  52.   define( 'ARCHIVE_ZIP_ERR_MISSING_FILE', -4 );
  53.   define( 'ARCHIVE_ZIP_ERR_FILENAME_TOO_LONG', -5 );
  54.   define( 'ARCHIVE_ZIP_ERR_INVALID_ZIP', -6 );
  55.   define( 'ARCHIVE_ZIP_ERR_BAD_EXTRACTED_FILE', -7 );
  56.   define( 'ARCHIVE_ZIP_ERR_DIR_CREATE_FAIL', -8 );
  57.   define( 'ARCHIVE_ZIP_ERR_BAD_EXTENSION', -9 );
  58.   define( 'ARCHIVE_ZIP_ERR_BAD_FORMAT', -10 );
  59.   define( 'ARCHIVE_ZIP_ERR_DELETE_FILE_FAIL', -11 );
  60.   define( 'ARCHIVE_ZIP_ERR_RENAME_FILE_FAIL', -12 );
  61.   define( 'ARCHIVE_ZIP_ERR_BAD_CHECKSUM', -13 );
  62.   define( 'ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
  63.   define( 'ARCHIVE_ZIP_ERR_MISSING_OPTION_VALUE', -15 );
  64.   define( 'ARCHIVE_ZIP_ERR_INVALID_PARAM_VALUE', -16 );
  65.  
  66.   // ----- Warning codes
  67.   define( 'ARCHIVE_ZIP_WARN_NO_WARNING', 0 );
  68.   define( 'ARCHIVE_ZIP_WARN_FILE_EXIST', 1 );
  69.  
  70.   // ----- Methods parameters
  71.   define( 'ARCHIVE_ZIP_PARAM_PATH', 'path' );
  72.   define( 'ARCHIVE_ZIP_PARAM_ADD_PATH', 'add_path' );
  73.   define( 'ARCHIVE_ZIP_PARAM_REMOVE_PATH', 'remove_path' );
  74.   define( 'ARCHIVE_ZIP_PARAM_REMOVE_ALL_PATH', 'remove_all_path' );
  75.   define( 'ARCHIVE_ZIP_PARAM_SET_CHMOD', 'set_chmod' );
  76.   define( 'ARCHIVE_ZIP_PARAM_EXTRACT_AS_STRING', 'extract_as_string' );
  77.   define( 'ARCHIVE_ZIP_PARAM_NO_COMPRESSION', 'no_compression' );
  78.   define( 'ARCHIVE_ZIP_PARAM_BY_NAME', 'by_name' );
  79.   define( 'ARCHIVE_ZIP_PARAM_BY_INDEX', 'by_index' );
  80.   define( 'ARCHIVE_ZIP_PARAM_BY_EREG', 'by_ereg' );
  81.   define( 'ARCHIVE_ZIP_PARAM_BY_PREG', 'by_preg' );
  82.  
  83.   define( 'ARCHIVE_ZIP_PARAM_PRE_EXTRACT', 'callback_pre_extract' );
  84.   define( 'ARCHIVE_ZIP_PARAM_POST_EXTRACT', 'callback_post_extract' );
  85.   define( 'ARCHIVE_ZIP_PARAM_PRE_ADD', 'callback_pre_add' );
  86.   define( 'ARCHIVE_ZIP_PARAM_POST_ADD', 'callback_post_add' );
  87.  
  88.  
  89.  
  90. /**
  91. * Class for manipulating zip archive files
  92. *
  93. * A class which provided common methods to manipulate ZIP formatted
  94. * archive files.
  95. * It provides creation, extraction, deletion and add features.
  96. *
  97. * @author   Vincent Blavet <vincent@blavet.net>
  98. * @version  $Revision: 1.2 $
  99. * @package  Archive_Zip
  100. * @category Archive
  101. */
  102. class Archive_Zip
  103. {
  104.     /**
  105.     * The filename of the zip archive.
  106.     *
  107.     * @var string Name of the Zip file
  108.     */
  109.     var $_zipname='';
  110.  
  111.     /**
  112.     * File descriptor of the opened Zip file.
  113.     *
  114.     * @var int Internal zip file descriptor
  115.     */
  116.     var $_zip_fd=0;
  117.  
  118.     /**
  119.     * @var int last error code
  120.     */
  121.     var $_error_code=1;
  122.  
  123.     /**
  124.     * @var string Last error description
  125.     */
  126.     var $_error_string='';
  127.  
  128.     // {{{ constructor
  129.     /**
  130.     * Archive_Zip Class constructor. This flavour of the constructor only
  131.     * declare a new Archive_Zip object, identifying it by the name of the
  132.     * zip file.
  133.     *
  134.     * @param    string  $p_zipname  The name of the zip archive to create
  135.     * @access public
  136.     */
  137.     function Archive_Zip($p_zipname)
  138.     {
  139.  
  140.       // ----- Check the zlib
  141.       if (!extension_loaded('zlib')) {
  142.           PEAR::loadExtension('zlib');
  143.       }
  144.       if (!extension_loaded('zlib')) {
  145.           die("The extension 'zlib' couldn't be found.\n".
  146.               "Please make sure your version of PHP was built ".
  147.               "with 'zlib' support.\n");
  148.           return false;
  149.       }
  150.  
  151.       // ----- Set the attributes
  152.       $this->_zipname = $p_zipname;
  153.       $this->_zip_fd = 0;
  154.  
  155.       return;
  156.     }
  157.     // }}}
  158.  
  159.     // {{{ create()
  160.     /**
  161.     * This method creates a Zip Archive with the filename set with
  162.     * the constructor.
  163.     * The files and directories indicated in $p_filelist
  164.     * are added in the archive.
  165.     * When a directory is in the list, the directory and its content is added
  166.     * in the archive.
  167.     * The methods takes a variable list of parameters in $p_params.
  168.     * The supported parameters for this method are :
  169.     *   'add_path' : Add a path to the archived files.
  170.     *   'remove_path' : Remove the specified 'root' path of the archived files.
  171.     *   'remove_all_path' : Remove all the path of the archived files.
  172.     *   'no_compression' : The archived files will not be compressed.
  173.     *
  174.     * @access public
  175.     * @param  mixed  $p_filelist  The list of the files or folders to add.
  176.     *                             It can be a string with filenames separated
  177.     *                             by a comma, or an array of filenames.
  178.     * @param  mixed  $p_params  An array of variable parameters and values.
  179.     * @return mixed An array of file description on success,
  180.     *               an error code on error
  181.     */
  182.     function create($p_filelist, $p_params=0)
  183.     {
  184.         $this->_errorReset();
  185.  
  186.         // ----- Set default values
  187.         if ($p_params === 0) {
  188.             $p_params = array();
  189.         }
  190.         if ($this->_check_parameters($p_params,
  191.                                      array('no_compression' => false,
  192.                                            'add_path' => "",
  193.                                            'remove_path' => "",
  194.                                            'remove_all_path' => false)) != 1) {
  195.             return 0;
  196.         }
  197.  
  198.         // ----- Look if the $p_filelist is really an array
  199.         $p_result_list = array();
  200.         if (is_array($p_filelist)) {
  201.             $v_result = $this->_create($p_filelist, $p_result_list, $p_params);
  202.         }
  203.  
  204.         // ----- Look if the $p_filelist is a string
  205.         else if (is_string($p_filelist)) {
  206.             // ----- Create a list with the elements from the string
  207.             $v_list = explode(ARCHIVE_ZIP_SEPARATOR, $p_filelist);
  208.  
  209.             $v_result = $this->_create($v_list, $p_result_list, $p_params);
  210.         }
  211.  
  212.         // ----- Invalid variable
  213.         else {
  214.             $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  215.                              'Invalid variable type p_filelist');
  216.             $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
  217.         }
  218.  
  219.         if ($v_result != 1) {
  220.             return 0;
  221.         }
  222.  
  223.         return $p_result_list;
  224.     }
  225.     // }}}
  226.  
  227.     // {{{ add()
  228.     /**
  229.     * This method add files or directory in an existing Zip Archive.
  230.     * If the Zip Archive does not exist it is created.
  231.     * The files and directories to add are indicated in $p_filelist.
  232.     * When a directory is in the list, the directory and its content is added
  233.     * in the archive.
  234.     * The methods takes a variable list of parameters in $p_params.
  235.     * The supported parameters for this method are :
  236.     *   'add_path' : Add a path to the archived files.
  237.     *   'remove_path' : Remove the specified 'root' path of the archived files.
  238.     *   'remove_all_path' : Remove all the path of the archived files.
  239.     *   'no_compression' : The archived files will not be compressed.
  240.     *   'callback_pre_add' : A callback function that will be called before
  241.     *                        each entry archiving.
  242.     *   'callback_post_add' : A callback function that will be called after
  243.     *                         each entry archiving.
  244.     *
  245.     * @access public
  246.     * @param    mixed  $p_filelist  The list of the files or folders to add.
  247.     *                               It can be a string with filenames separated
  248.     *                               by a comma, or an array of filenames.
  249.     * @param    mixed  $p_params  An array of variable parameters and values.
  250.     * @return mixed An array of file description on success,
  251.     *               0 on an unrecoverable failure, an error code is logged.
  252.     */
  253.     function add($p_filelist, $p_params=0)
  254.     {
  255.         $this->_errorReset();
  256.  
  257.         // ----- Set default values
  258.         if ($p_params === 0) {
  259.             $p_params = array();
  260.         }
  261.         if ($this->_check_parameters($p_params,
  262.                                      array ('no_compression' => false,
  263.                                             'add_path' => '',
  264.                                             'remove_path' => '',
  265.                                             'remove_all_path' => false,
  266.                                              'callback_pre_add' => '',
  267.                                             'callback_post_add' => '')) != 1) {
  268.             return 0;
  269.         }
  270.  
  271.         // ----- Look if the $p_filelist is really an array
  272.         $p_result_list = array();
  273.         if (is_array($p_filelist)) {
  274.             // ----- Call the create fct
  275.             $v_result = $this->_add($p_filelist, $p_result_list, $p_params);
  276.         }
  277.  
  278.         // ----- Look if the $p_filelist is a string
  279.         else if (is_string($p_filelist)) {
  280.             // ----- Create a list with the elements from the string
  281.             $v_list = explode(ARCHIVE_ZIP_SEPARATOR, $p_filelist);
  282.  
  283.             // ----- Call the create fct
  284.             $v_result = $this->_add($v_list, $p_result_list, $p_params);
  285.         }
  286.  
  287.         // ----- Invalid variable
  288.         else {
  289.             $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  290.                              "add() : Invalid variable type p_filelist");
  291.             $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
  292.         }
  293.  
  294.         if ($v_result != 1) {
  295.             return 0;
  296.         }
  297.  
  298.         // ----- Return the result list
  299.         return $p_result_list;
  300.     }
  301.     // }}}
  302.  
  303.     // {{{ listContent()
  304.     /**
  305.     * This method gives the names and properties of the files and directories
  306.     * which are present in the zip archive.
  307.     * The properties of each entries in the list are :
  308.     *   filename : Name of the file.
  309.     *              For create() or add() it's the filename given by the user.
  310.     *              For an extract() it's the filename of the extracted file.
  311.     *   stored_filename : Name of the file / directory stored in the archive.
  312.     *   size : Size of the stored file.
  313.     *   compressed_size : Size of the file's data compressed in the archive
  314.     *                     (without the zip headers overhead)
  315.     *   mtime : Last known modification date of the file (UNIX timestamp)
  316.     *   comment : Comment associated with the file
  317.     *   folder : true | false (indicates if the entry is a folder)
  318.     *   index : index of the file in the archive (-1 when not available)
  319.     *   status : status of the action on the entry (depending of the action) :
  320.     *            Values are :
  321.     *              ok : OK !
  322.     *              filtered : the file/dir was not extracted (filtered by user)
  323.     *              already_a_directory : the file can't be extracted because a
  324.     *                                    directory with the same name already
  325.     *                                    exists
  326.     *              write_protected : the file can't be extracted because a file
  327.     *                                with the same name already exists and is
  328.     *                                write protected
  329.     *              newer_exist : the file was not extracted because a newer
  330.     *                            file already exists
  331.     *              path_creation_fail : the file is not extracted because the
  332.     *                                   folder does not exists and can't be
  333.     *                                   created
  334.     *              write_error : the file was not extracted because there was a
  335.     *                            error while writing the file
  336.     *              read_error : the file was not extracted because there was a
  337.     *                           error while reading the file
  338.     *              invalid_header : the file was not extracted because of an
  339.     *                               archive format error (bad file header)
  340.     * Note that each time a method can continue operating when there
  341.     * is an error on a single file, the error is only logged in the file status.
  342.     *
  343.     * @access public
  344.     * @return mixed An array of file description on success,
  345.     *               0 on an unrecoverable failure, an error code is logged.
  346.     */
  347.     function listContent()
  348.     {
  349.         $this->_errorReset();
  350.  
  351.         // ----- Check archive
  352.         if (!$this->_checkFormat()) {
  353.             return(0);
  354.         }
  355.  
  356.         $v_list = array();
  357.         if ($this->_list($v_list) != 1) {
  358.             unset($v_list);
  359.             return(0);
  360.         }
  361.  
  362.         return $v_list;
  363.     }
  364.     // }}}
  365.  
  366.     // {{{ extract()
  367.     /**
  368.     * This method extract the files and folders which are in the zip archive.
  369.     * It can extract all the archive or a part of the archive by using filter
  370.     * feature (extract by name, by index, by ereg, by preg). The extraction
  371.     * can occur in the current path or an other path.
  372.     * All the advanced features are activated by the use of variable
  373.     * parameters.
  374.     * The return value is an array of entry descriptions which gives
  375.     * information on extracted files (See listContent()).
  376.     * The method may return a success value (an array) even if some files
  377.     * are not correctly extracted (see the file status in listContent()).
  378.     * The supported variable parameters for this method are :
  379.     *   'add_path' : Path where the files and directories are to be extracted
  380.     *   'remove_path' : First part ('root' part) of the memorized path
  381.     *                   (if similar) to remove while extracting.
  382.     *   'remove_all_path' : Remove all the memorized path while extracting.
  383.     *   'extract_as_string' :
  384.     *   'set_chmod' : After the extraction of the file the indicated mode
  385.     *                 will be set.
  386.     *   'by_name' : It can be a string with file/dir names separated by ',',
  387.     *               or an array of file/dir names to extract from the archive.
  388.     *   'by_index' : A string with range of indexes separated by ',',
  389.     *                (sample "1,3-5,12").
  390.     *   'by_ereg' : A regular expression (ereg) that must match the extracted
  391.     *               filename.
  392.     *   'by_preg' : A regular expression (preg) that must match the extracted
  393.     *               filename.
  394.     *   'callback_pre_extract' : A callback function that will be called before
  395.     *                            each entry extraction.
  396.     *   'callback_post_extract' : A callback function that will be called after
  397.     *                            each entry extraction.
  398.     *
  399.     * @access public
  400.     * @param    mixed  $p_params  An array of variable parameters and values.
  401.     * @return mixed An array of file description on success,
  402.     *               0 on an unrecoverable failure, an error code is logged.
  403.     */
  404.     function extract($p_params=0)
  405.     {
  406.  
  407.         $this->_errorReset();
  408.  
  409.         // ----- Check archive
  410.         if (!$this->_checkFormat()) {
  411.             return(0);
  412.         }
  413.  
  414.         // ----- Set default values
  415.         if ($p_params === 0) {
  416.             $p_params = array();
  417.         }
  418.         if ($this->_check_parameters($p_params,
  419.                                      array ('extract_as_string' => false,
  420.                                             'add_path' => '',
  421.                                             'remove_path' => '',
  422.                                             'remove_all_path' => false,
  423.                                              'callback_pre_extract' => '',
  424.                                             'callback_post_extract' => '',
  425.                                             'set_chmod' => 0,
  426.                                             'by_name' => '',
  427.                                             'by_index' => '',
  428.                                             'by_ereg' => '',
  429.                                             'by_preg' => '') ) != 1) {
  430.             return 0;
  431.         }
  432.  
  433.         // ----- Call the extracting fct
  434.         $v_list = array();
  435.         if ($this->_extractByRule($v_list, $p_params) != 1) {
  436.             unset($v_list);
  437.             return(0);
  438.         }
  439.  
  440.         return $v_list;
  441.     }
  442.     // }}}
  443.  
  444.  
  445.     // {{{ delete()
  446.     /**
  447.     * This methods delete archive entries in the zip archive.
  448.     * Notice that at least one filtering rule (set by the variable parameter
  449.     * list) must be set.
  450.     * Also notice that if you delete a folder entry, only the folder entry
  451.     * is deleted, not all the files bellonging to this folder.
  452.     * The supported variable parameters for this method are :
  453.     *   'by_name' : It can be a string with file/dir names separated by ',',
  454.     *               or an array of file/dir names to delete from the archive.
  455.     *   'by_index' : A string with range of indexes separated by ',',
  456.     *                (sample "1,3-5,12").
  457.     *   'by_ereg' : A regular expression (ereg) that must match the extracted
  458.     *               filename.
  459.     *   'by_preg' : A regular expression (preg) that must match the extracted
  460.     *               filename.
  461.     *
  462.     * @access public
  463.     * @param    mixed  $p_params  An array of variable parameters and values.
  464.     * @return mixed An array of file description on success,
  465.     *               0 on an unrecoverable failure, an error code is logged.
  466.     */
  467.     function delete($p_params)
  468.     {
  469.         $this->_errorReset();
  470.  
  471.         // ----- Check archive
  472.         if (!$this->_checkFormat()) {
  473.             return(0);
  474.         }
  475.  
  476.         // ----- Set default values
  477.         if ($this->_check_parameters($p_params,
  478.                                      array ('by_name' => '',
  479.                                             'by_index' => '',
  480.                                             'by_ereg' => '',
  481.                                             'by_preg' => '') ) != 1) {
  482.             return 0;
  483.         }
  484.  
  485.         // ----- Check that at least one rule is set
  486.         if (   ($p_params['by_name'] == '')
  487.             && ($p_params['by_index'] == '')
  488.             && ($p_params['by_ereg'] == '')
  489.             && ($p_params['by_preg'] == '')) {
  490.             $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  491.                              'At least one filtering rule must'
  492.                              .' be set as parameter');
  493.             return 0;
  494.         }
  495.  
  496.         // ----- Call the delete fct
  497.         $v_list = array();
  498.         if ($this->_deleteByRule($v_list, $p_params) != 1) {
  499.             unset($v_list);
  500.             return(0);
  501.         }
  502.  
  503.         return $v_list;
  504.     }
  505.     // }}}
  506.  
  507.     // {{{ properties()
  508.     /**
  509.     * This method gives the global properties of the archive.
  510.     *  The properties are :
  511.     *    nb : Number of files in the archive
  512.     *    comment : Comment associated with the archive file
  513.     *    status : not_exist, ok
  514.     *
  515.     * @access public
  516.     * @param    mixed  $p_params  {Description}
  517.     * @return mixed An array with the global properties or 0 on error.
  518.     */
  519.     function properties()
  520.     {
  521.         $this->_errorReset();
  522.  
  523.         // ----- Check archive
  524.         if (!$this->_checkFormat()) {
  525.             return(0);
  526.         }
  527.  
  528.         // ----- Default properties
  529.         $v_prop = array();
  530.         $v_prop['comment'] = '';
  531.         $v_prop['nb'] = 0;
  532.         $v_prop['status'] = 'not_exist';
  533.  
  534.         // ----- Look if file exists
  535.         if (@is_file($this->_zipname)) {
  536.             // ----- Open the zip file
  537.             if (($this->_zip_fd = @fopen($this->_zipname, 'rb')) == 0) {
  538.                 $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  539.                                  'Unable to open archive \''.$this->_zipname
  540.                                  .'\' in binary read mode');
  541.                 return 0;
  542.             }
  543.  
  544.             // ----- Read the central directory informations
  545.             $v_central_dir = array();
  546.             if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
  547.                 return 0;
  548.             }
  549.  
  550.             $this->_closeFd();
  551.  
  552.             // ----- Set the user attributes
  553.             $v_prop['comment'] = $v_central_dir['comment'];
  554.             $v_prop['nb'] = $v_central_dir['entries'];
  555.             $v_prop['status'] = 'ok';
  556.         }
  557.  
  558.         return $v_prop;
  559.     }
  560.     // }}}
  561.  
  562.  
  563.     // {{{ duplicate()
  564.     /**
  565.     * This method creates an archive by copying the content of an other one.
  566.     * If the archive already exist, it is replaced by the new one without
  567.     * any warning.
  568.     *
  569.     * @access public
  570.     * @param  mixed  $p_archive  It can be a valid Archive_Zip object or
  571.     *                            the filename of a valid zip archive.
  572.     * @return integer 1 on success, 0 on failure.
  573.     */
  574.     function duplicate($p_archive)
  575.     {
  576.         $this->_errorReset();
  577.  
  578.         // ----- Look if the $p_archive is a Archive_Zip object
  579.         if (   (is_object($p_archive))
  580.             && (strtolower(get_class($p_archive)) == 'archive_zip')) {
  581.             $v_result = $this->_duplicate($p_archive->_zipname);
  582.         }
  583.  
  584.         // ----- Look if the $p_archive is a string (so a filename)
  585.         else if (is_string($p_archive)) {
  586.             // ----- Check that $p_archive is a valid zip file
  587.             // TBC : Should also check the archive format
  588.             if (!is_file($p_archive)) {
  589.                 $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
  590.                                  "No file with filename '".$p_archive."'");
  591.                 $v_result = ARCHIVE_ZIP_ERR_MISSING_FILE;
  592.             }
  593.             else {
  594.                 $v_result = $this->_duplicate($p_archive);
  595.             }
  596.         }
  597.  
  598.         // ----- Invalid variable
  599.         else {
  600.             $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  601.                              "Invalid variable type p_archive_to_add");
  602.             $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
  603.         }
  604.  
  605.         return $v_result;
  606.     }
  607.     // }}}
  608.  
  609.     // {{{ merge()
  610.     /**
  611.     *  This method merge a valid zip archive at the end of the
  612.     *  archive identified by the Archive_Zip object.
  613.     *  If the archive ($this) does not exist, the merge becomes a duplicate.
  614.     *  If the archive to add does not exist, the merge is a success.
  615.     *
  616.     * @access public
  617.     * @param mixed $p_archive_to_add  It can be a valid Archive_Zip object or
  618.     *                                 the filename of a valid zip archive.
  619.     * @return integer 1 on success, 0 on failure.
  620.     */
  621.     function merge($p_archive_to_add)
  622.     {
  623.         $v_result = 1;
  624.         $this->_errorReset();
  625.  
  626.         // ----- Check archive
  627.         if (!$this->_checkFormat()) {
  628.             return(0);
  629.         }
  630.  
  631.         // ----- Look if the $p_archive_to_add is a Archive_Zip object
  632.         if (   (is_object($p_archive_to_add))
  633.             && (strtolower(get_class($p_archive_to_add)) == 'archive_zip')) {
  634.             $v_result = $this->_merge($p_archive_to_add);
  635.         }
  636.  
  637.         // ----- Look if the $p_archive_to_add is a string (so a filename)
  638.         else if (is_string($p_archive_to_add)) {
  639.             // ----- Create a temporary archive
  640.             $v_object_archive = new Archive_Zip($p_archive_to_add);
  641.  
  642.             // ----- Merge the archive
  643.             $v_result = $this->_merge($v_object_archive);
  644.         }
  645.  
  646.         // ----- Invalid variable
  647.         else {
  648.             $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  649.                              "Invalid variable type p_archive_to_add");
  650.             $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
  651.         }
  652.  
  653.         return $v_result;
  654.     }
  655.     // }}}
  656.  
  657.     // {{{ errorCode()
  658.     /**
  659.     * Method that gives the lastest error code.
  660.     *
  661.     * @access public
  662.     * @return integer The error code value.
  663.     */
  664.     function errorCode()
  665.     {
  666.         return($this->_error_code);
  667.     }
  668.     // }}}
  669.  
  670.     // {{{ errorName()
  671.     /**
  672.     * This method gives the latest error code name.
  673.     *
  674.     * @access public
  675.     * @param  boolean $p_with_code  If true, gives the name and the int value.
  676.     * @return string The error name.
  677.     */
  678.     function errorName($p_with_code=false)
  679.     {
  680.         $v_const_list = get_defined_constants();
  681.       
  682.           // ----- Extract error constants from all const.
  683.         for (reset($v_const_list);
  684.              list($v_key, $v_value) = each($v_const_list);) {
  685.              if (substr($v_key, 0, strlen('ARCHIVE_ZIP_ERR_'))
  686.                 =='ARCHIVE_ZIP_ERR_') {
  687.                 $v_error_list[$v_key] = $v_value;
  688.             }
  689.         }
  690.     
  691.         // ----- Search the name form the code value
  692.         $v_key=array_search($this->_error_code, $v_error_list, true);
  693.           if ($v_key!=false) {
  694.             $v_value = $v_key;
  695.           }
  696.           else {
  697.             $v_value = 'NoName';
  698.           }
  699.       
  700.         if ($p_with_code) {
  701.             return($v_value.' ('.$this->_error_code.')');
  702.         }
  703.         else {
  704.           return($v_value);
  705.         }
  706.     }
  707.     // }}}
  708.  
  709.     // {{{ errorInfo()
  710.     /**
  711.     * This method returns the description associated with the latest error.
  712.     *
  713.     * @access public
  714.     * @param  boolean $p_full If set to true gives the description with the
  715.     *                         error code, the name and the description.
  716.     *                         If set to false gives only the description
  717.     *                         and the error code.
  718.     * @return string The error description.
  719.     */
  720.     function errorInfo($p_full=false)
  721.     {
  722.         if ($p_full) {
  723.             return($this->errorName(true)." : ".$this->_error_string);
  724.         }
  725.         else {
  726.             return($this->_error_string." [code ".$this->_error_code."]");
  727.         }
  728.     }
  729.     // }}}
  730.  
  731.  
  732. // -----------------------------------------------------------------------------
  733. // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
  734. // *****                                                        *****
  735. // *****       THESES FUNCTIONS MUST NOT BE USED DIRECTLY       *****
  736. // -----------------------------------------------------------------------------
  737.  
  738.   // ---------------------------------------------------------------------------
  739.   // Function : _checkFormat()
  740.   // Description :
  741.   //   This method check that the archive exists and is a valid zip archive.
  742.   //   Several level of check exists. (futur)
  743.   // Parameters :
  744.   //   $p_level : Level of check. Default 0.
  745.   //              0 : Check the first bytes (magic codes) (default value))
  746.   //              1 : 0 + Check the central directory (futur)
  747.   //              2 : 1 + Check each file header (futur)
  748.   // Return Values :
  749.   //   true on success,
  750.   //   false on error, the error code is set.
  751.   // ---------------------------------------------------------------------------
  752.   /**
  753.   * Archive_Zip::_checkFormat()
  754.   *
  755.   * { Description }
  756.   *
  757.   * @param integer $p_level
  758.   */
  759.   function _checkFormat($p_level=0)
  760.   {
  761.     $v_result = true;
  762.  
  763.     // ----- Reset the error handler
  764.     $this->_errorReset();
  765.  
  766.     // ----- Look if the file exits
  767.     if (!is_file($this->_zipname)) {
  768.       // ----- Error log
  769.       $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
  770.                        "Missing archive file '".$this->_zipname."'");
  771.       return(false);
  772.     }
  773.  
  774.     // ----- Check that the file is readeable
  775.     if (!is_readable($this->_zipname)) {
  776.       // ----- Error log
  777.       $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  778.                        "Unable to read archive '".$this->_zipname."'");
  779.       return(false);
  780.     }
  781.  
  782.     // ----- Check the magic code
  783.     // TBC
  784.  
  785.     // ----- Check the central header
  786.     // TBC
  787.  
  788.     // ----- Check each file header
  789.     // TBC
  790.  
  791.     // ----- Return
  792.     return $v_result;
  793.   }
  794.   // ---------------------------------------------------------------------------
  795.  
  796.   // ---------------------------------------------------------------------------
  797.   // Function : _create()
  798.   // Description :
  799.   // Parameters :
  800.   // Return Values :
  801.   // ---------------------------------------------------------------------------
  802.   /**
  803.   * Archive_Zip::_create()
  804.   *
  805.   * { Description }
  806.   *
  807.   */
  808.   function _create($p_list, &$p_result_list, &$p_params)
  809.   {
  810.     $v_result=1;
  811.     $v_list_detail = array();
  812.  
  813.     $p_add_dir = $p_params['add_path'];
  814.     $p_remove_dir = $p_params['remove_path'];
  815.     $p_remove_all_dir = $p_params['remove_all_path'];
  816.  
  817.     // ----- Open the file in write mode
  818.     if (($v_result = $this->_openFd('wb')) != 1)
  819.     {
  820.       // ----- Return
  821.       return $v_result;
  822.     }
  823.  
  824.     // ----- Add the list of files
  825.     $v_result = $this->_addList($p_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params);
  826.  
  827.     // ----- Close
  828.     $this->_closeFd();
  829.  
  830.     // ----- Return
  831.     return $v_result;
  832.   }
  833.   // ---------------------------------------------------------------------------
  834.  
  835.   // ---------------------------------------------------------------------------
  836.   // Function : _add()
  837.   // Description :
  838.   // Parameters :
  839.   // Return Values :
  840.   // ---------------------------------------------------------------------------
  841.   /**
  842.   * Archive_Zip::_add()
  843.   *
  844.   * { Description }
  845.   *
  846.   */
  847.   function _add($p_list, &$p_result_list, &$p_params)
  848.   {
  849.     $v_result=1;
  850.     $v_list_detail = array();
  851.  
  852.     $p_add_dir = $p_params['add_path'];
  853.     $p_remove_dir = $p_params['remove_path'];
  854.     $p_remove_all_dir = $p_params['remove_all_path'];
  855.  
  856.     // ----- Look if the archive exists or is empty and need to be created
  857.     if ((!is_file($this->_zipname)) || (filesize($this->_zipname) == 0)) {
  858.       $v_result = $this->_create($p_list, $p_result_list, $p_params);
  859.       return $v_result;
  860.     }
  861.  
  862.     // ----- Open the zip file
  863.     if (($v_result=$this->_openFd('rb')) != 1) {
  864.       return $v_result;
  865.     }
  866.  
  867.     // ----- Read the central directory informations
  868.     $v_central_dir = array();
  869.     if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1)
  870.     {
  871.       $this->_closeFd();
  872.       return $v_result;
  873.     }
  874.  
  875.     // ----- Go to beginning of File
  876.     @rewind($this->_zip_fd);
  877.  
  878.     // ----- Creates a temporay file
  879.     $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR.uniqid('archive_zip-').'.tmp';
  880.  
  881.     // ----- Open the temporary file in write mode
  882.     if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  883.     {
  884.       $this->_closeFd();
  885.  
  886.       $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  887.                        'Unable to open temporary file \''
  888.                        .$v_zip_temp_name.'\' in binary write mode');
  889.       return Archive_Zip::errorCode();
  890.     }
  891.  
  892.     // ----- Copy the files from the archive to the temporary file
  893.     // TBC : Here I should better append the file and go back to erase the
  894.     // central dir
  895.     $v_size = $v_central_dir['offset'];
  896.     while ($v_size != 0)
  897.     {
  898.       $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  899.                       ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  900.       $v_buffer = fread($this->_zip_fd, $v_read_size);
  901.       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  902.       $v_size -= $v_read_size;
  903.     }
  904.  
  905.     // ----- Swap the file descriptor
  906.     // Here is a trick : I swap the temporary fd with the zip fd, in order to 
  907.     // use the following methods on the temporary fil and not the real archive
  908.     $v_swap = $this->_zip_fd;
  909.     $this->_zip_fd = $v_zip_temp_fd;
  910.     $v_zip_temp_fd = $v_swap;
  911.  
  912.     // ----- Add the files
  913.     $v_header_list = array();
  914.     if (($v_result = $this->_addFileList($p_list, $v_header_list,
  915.                                          $p_add_dir, $p_remove_dir,
  916.                                          $p_remove_all_dir, $p_params)) != 1)
  917.     {
  918.       fclose($v_zip_temp_fd);
  919.       $this->_closeFd();
  920.       @unlink($v_zip_temp_name);
  921.  
  922.       // ----- Return
  923.       return $v_result;
  924.     }
  925.  
  926.     // ----- Store the offset of the central dir
  927.     $v_offset = @ftell($this->_zip_fd);
  928.  
  929.     // ----- Copy the block of file headers from the old archive
  930.     $v_size = $v_central_dir['size'];
  931.     while ($v_size != 0)
  932.     {
  933.       $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  934.                       ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  935.       $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  936.       @fwrite($this->_zip_fd, $v_buffer, $v_read_size);
  937.       $v_size -= $v_read_size;
  938.     }
  939.  
  940.     // ----- Create the Central Dir files header
  941.     for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++)
  942.     {
  943.       // ----- Create the file header
  944.       if ($v_header_list[$i]['status'] == 'ok') {
  945.         if (($v_result=$this->_writeCentralFileHeader($v_header_list[$i]))!=1) {
  946.           fclose($v_zip_temp_fd);
  947.           $this->_closeFd();
  948.           @unlink($v_zip_temp_name);
  949.  
  950.           // ----- Return
  951.           return $v_result;
  952.         }
  953.         $v_count++;
  954.       }
  955.  
  956.       // ----- Transform the header to a 'usable' info
  957.       $this->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  958.     }
  959.  
  960.     // ----- Zip file comment
  961.     $v_comment = '';
  962.  
  963.     // ----- Calculate the size of the central header
  964.     $v_size = @ftell($this->_zip_fd)-$v_offset;
  965.  
  966.     // ----- Create the central dir footer
  967.     if (($v_result = $this->_writeCentralHeader($v_count
  968.                                                   +$v_central_dir['entries'],
  969.                                                 $v_size, $v_offset,
  970.                                                 $v_comment)) != 1) {
  971.       // ----- Reset the file list
  972.       unset($v_header_list);
  973.  
  974.       // ----- Return
  975.       return $v_result;
  976.     }
  977.  
  978.     // ----- Swap back the file descriptor
  979.     $v_swap = $this->_zip_fd;
  980.     $this->_zip_fd = $v_zip_temp_fd;
  981.     $v_zip_temp_fd = $v_swap;
  982.  
  983.     // ----- Close
  984.     $this->_closeFd();
  985.  
  986.     // ----- Close the temporary file
  987.     @fclose($v_zip_temp_fd);
  988.  
  989.     // ----- Delete the zip file
  990.     // TBC : I should test the result ...
  991.     @unlink($this->_zipname);
  992.  
  993.     // ----- Rename the temporary file
  994.     // TBC : I should test the result ...
  995.     //@rename($v_zip_temp_name, $this->_zipname);
  996.     $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
  997.  
  998.     // ----- Return
  999.     return $v_result;
  1000.   }
  1001.   // ---------------------------------------------------------------------------
  1002.  
  1003.   // ---------------------------------------------------------------------------
  1004.   // Function : _openFd()
  1005.   // Description :
  1006.   // Parameters :
  1007.   // ---------------------------------------------------------------------------
  1008.   /**
  1009.   * Archive_Zip::_openFd()
  1010.   *
  1011.   * { Description }
  1012.   *
  1013.   */
  1014.   function _openFd($p_mode)
  1015.   {
  1016.     $v_result=1;
  1017.  
  1018.     // ----- Look if already open
  1019.     if ($this->_zip_fd != 0)
  1020.     {
  1021.       $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  1022.                        'Zip file \''.$this->_zipname.'\' already open');
  1023.       return Archive_Zip::errorCode();
  1024.     }
  1025.  
  1026.     // ----- Open the zip file
  1027.     if (($this->_zip_fd = @fopen($this->_zipname, $p_mode)) == 0)
  1028.     {
  1029.       $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  1030.                        'Unable to open archive \''.$this->_zipname
  1031.                        .'\' in '.$p_mode.' mode');
  1032.       return Archive_Zip::errorCode();
  1033.     }
  1034.  
  1035.     // ----- Return
  1036.     return $v_result;
  1037.   }
  1038.   // ---------------------------------------------------------------------------
  1039.  
  1040.   // ---------------------------------------------------------------------------
  1041.   // Function : _closeFd()
  1042.   // Description :
  1043.   // Parameters :
  1044.   // ---------------------------------------------------------------------------
  1045.   /**
  1046.   * Archive_Zip::_closeFd()
  1047.   *
  1048.   * { Description }
  1049.   *
  1050.   */
  1051.   function _closeFd()
  1052.   {
  1053.     $v_result=1;
  1054.  
  1055.     if ($this->_zip_fd != 0)
  1056.       @fclose($this->_zip_fd);
  1057.     $this->_zip_fd = 0;
  1058.  
  1059.     // ----- Return
  1060.     return $v_result;
  1061.   }
  1062.   // ---------------------------------------------------------------------------
  1063.  
  1064.   // ---------------------------------------------------------------------------
  1065.   // Function : _addList()
  1066.   // Description :
  1067.   //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  1068.   //   different from the real path of the file. This is usefull if you want to have PclTar
  1069.   //   running in any directory, and memorize relative path from an other directory.
  1070.   // Parameters :
  1071.   //   $p_list : An array containing the file or directory names to add in the tar
  1072.   //   $p_result_list : list of added files with their properties (specially the status field)
  1073.   //   $p_add_dir : Path to add in the filename path archived
  1074.   //   $p_remove_dir : Path to remove in the filename path archived
  1075.   // Return Values :
  1076.   // ---------------------------------------------------------------------------
  1077.   /**
  1078.   * Archive_Zip::_addList()
  1079.   *
  1080.   * { Description }
  1081.   *
  1082.   */
  1083.   function _addList($p_list, &$p_result_list,
  1084.                     $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_params)
  1085.   {
  1086.     $v_result=1;
  1087.  
  1088.     // ----- Add the files
  1089.     $v_header_list = array();
  1090.     if (($v_result = $this->_addFileList($p_list, $v_header_list,
  1091.                                          $p_add_dir, $p_remove_dir,
  1092.                                          $p_remove_all_dir, $p_params)) != 1) {
  1093.       return $v_result;
  1094.     }
  1095.  
  1096.     // ----- Store the offset of the central dir
  1097.     $v_offset = @ftell($this->_zip_fd);
  1098.  
  1099.     // ----- Create the Central Dir files header
  1100.     for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
  1101.     {
  1102.       // ----- Create the file header
  1103.       if ($v_header_list[$i]['status'] == 'ok') {
  1104.         if (($v_result = $this->_writeCentralFileHeader($v_header_list[$i])) != 1) {
  1105.           return $v_result;
  1106.         }
  1107.         $v_count++;
  1108.       }
  1109.  
  1110.       // ----- Transform the header to a 'usable' info
  1111.       $this->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1112.     }
  1113.  
  1114.     // ----- Zip file comment
  1115.     $v_comment = '';
  1116.  
  1117.     // ----- Calculate the size of the central header
  1118.     $v_size = @ftell($this->_zip_fd)-$v_offset;
  1119.  
  1120.     // ----- Create the central dir footer
  1121.     if (($v_result = $this->_writeCentralHeader($v_count, $v_size, $v_offset,
  1122.                                                 $v_comment)) != 1)
  1123.     {
  1124.       // ----- Reset the file list
  1125.       unset($v_header_list);
  1126.  
  1127.       // ----- Return
  1128.       return $v_result;
  1129.     }
  1130.  
  1131.     // ----- Return
  1132.     return $v_result;
  1133.   }
  1134.   // ---------------------------------------------------------------------------
  1135.  
  1136.   // ---------------------------------------------------------------------------
  1137.   // Function : _addFileList()
  1138.   // Description :
  1139.   //   $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  1140.   //   different from the real path of the file. This is usefull if you want to
  1141.   //   run the lib in any directory, and memorize relative path from an other directory.
  1142.   // Parameters :
  1143.   //   $p_list : An array containing the file or directory names to add in the tar
  1144.   //   $p_result_list : list of added files with their properties (specially the status field)
  1145.   //   $p_add_dir : Path to add in the filename path archived
  1146.   //   $p_remove_dir : Path to remove in the filename path archived
  1147.   // Return Values :
  1148.   // ---------------------------------------------------------------------------
  1149.   /**
  1150.   * Archive_Zip::_addFileList()
  1151.   *
  1152.   * { Description }
  1153.   *
  1154.   */
  1155.   function _addFileList($p_list, &$p_result_list,
  1156.                         $p_add_dir, $p_remove_dir, $p_remove_all_dir,
  1157.                         &$p_params)
  1158.   {
  1159.     $v_result=1;
  1160.     $v_header = array();
  1161.  
  1162.     // ----- Recuperate the current number of elt in list
  1163.     $v_nb = sizeof($p_result_list);
  1164.  
  1165.     // ----- Loop on the files
  1166.     for ($j=0; ($j<count($p_list)) && ($v_result==1); $j++)
  1167.     {
  1168.       // ----- Recuperate the filename
  1169.       $p_filename = $this->_tool_TranslateWinPath($p_list[$j], false);
  1170.  
  1171.       // ----- Skip empty file names
  1172.       if ($p_filename == "")
  1173.       {
  1174.         continue;
  1175.       }
  1176.  
  1177.       // ----- Check the filename
  1178.       if (!file_exists($p_filename))
  1179.       {
  1180.         $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
  1181.                          "File '$p_filename' does not exists");
  1182.         return Archive_Zip::errorCode();
  1183.       }
  1184.  
  1185.       // ----- Look if it is a file or a dir with no all pathnre move
  1186.       if ((is_file($p_filename)) || ((is_dir($p_filename)) && !$p_remove_all_dir)) {
  1187.         // ----- Add the file
  1188.         if (($v_result = $this->_addFile($p_filename, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params)) != 1)
  1189.         {
  1190.           // ----- Return status
  1191.           return $v_result;
  1192.         }
  1193.  
  1194.         // ----- Store the file infos
  1195.         $p_result_list[$v_nb++] = $v_header;
  1196.       }
  1197.  
  1198.       // ----- Look for directory
  1199.       if (is_dir($p_filename))
  1200.       {
  1201.  
  1202.         // ----- Look for path
  1203.         if ($p_filename != ".")
  1204.           $v_path = $p_filename."/";
  1205.         else
  1206.           $v_path = "";
  1207.  
  1208.         // ----- Read the directory for files and sub-directories
  1209.         $p_hdir = opendir($p_filename);
  1210.         $p_hitem = readdir($p_hdir); // '.' directory
  1211.         $p_hitem = readdir($p_hdir); // '..' directory
  1212.         while ($p_hitem = readdir($p_hdir))
  1213.         {
  1214.  
  1215.           // ----- Look for a file
  1216.           if (is_file($v_path.$p_hitem))
  1217.           {
  1218.  
  1219.             // ----- Add the file
  1220.             if (($v_result = $this->_addFile($v_path.$p_hitem, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params)) != 1)
  1221.             {
  1222.               // ----- Return status
  1223.               return $v_result;
  1224.             }
  1225.  
  1226.             // ----- Store the file infos
  1227.             $p_result_list[$v_nb++] = $v_header;
  1228.           }
  1229.  
  1230.           // ----- Recursive call to _addFileList()
  1231.           else
  1232.           {
  1233.  
  1234.             // ----- Need an array as parameter
  1235.             $p_temp_list[0] = $v_path.$p_hitem;
  1236.             $v_result = $this->_addFileList($p_temp_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params);
  1237.  
  1238.             // ----- Update the number of elements of the list
  1239.             $v_nb = sizeof($p_result_list);
  1240.           }
  1241.         }
  1242.  
  1243.         // ----- Free memory for the recursive loop
  1244.         unset($p_temp_list);
  1245.         unset($p_hdir);
  1246.         unset($p_hitem);
  1247.       }
  1248.     }
  1249.  
  1250.     return $v_result;
  1251.   }
  1252.   // ---------------------------------------------------------------------------
  1253.  
  1254.   // ---------------------------------------------------------------------------
  1255.   // Function : _addFile()
  1256.   // Description :
  1257.   // Parameters :
  1258.   // Return Values :
  1259.   // ---------------------------------------------------------------------------
  1260.   /**
  1261.   * Archive_Zip::_addFile()
  1262.   *
  1263.   * { Description }
  1264.   *
  1265.   */
  1266.   function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_params)
  1267.   {
  1268.     $v_result=1;
  1269.  
  1270.     if ($p_filename == "")
  1271.     {
  1272.       // ----- Error log
  1273.       $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  1274.  
  1275.       // ----- Return
  1276.       return Archive_Zip::errorCode();
  1277.     }
  1278.  
  1279.     // ----- Calculate the stored filename
  1280.     $v_stored_filename = $p_filename;
  1281.  
  1282.     // ----- Look for all path to remove
  1283.     if ($p_remove_all_dir) {
  1284.       $v_stored_filename = basename($p_filename);
  1285.     }
  1286.     // ----- Look for partial path remove
  1287.     else if ($p_remove_dir != "")
  1288.     {
  1289.       if (substr($p_remove_dir, -1) != '/')
  1290.         $p_remove_dir .= "/";
  1291.  
  1292.       if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./"))
  1293.       {
  1294.         if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./"))
  1295.           $p_remove_dir = "./".$p_remove_dir;
  1296.         if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./"))
  1297.           $p_remove_dir = substr($p_remove_dir, 2);
  1298.       }
  1299.  
  1300.       $v_compare = $this->_tool_PathInclusion($p_remove_dir, $p_filename);
  1301.       if ($v_compare > 0)
  1302. //      if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
  1303.       {
  1304.  
  1305.         if ($v_compare == 2) {
  1306.           $v_stored_filename = "";
  1307.         }
  1308.         else {
  1309.           $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  1310.         }
  1311.       }
  1312.     }
  1313.     // ----- Look for path to add
  1314.     if ($p_add_dir != "")
  1315.     {
  1316.       if (substr($p_add_dir, -1) == "/")
  1317.         $v_stored_filename = $p_add_dir.$v_stored_filename;
  1318.       else
  1319.         $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  1320.     }
  1321.  
  1322.     // ----- Filename (reduce the path of stored name)
  1323.     $v_stored_filename = $this->_tool_PathReduction($v_stored_filename);
  1324.  
  1325.  
  1326.     /* filename length moved after call-back in release 1.3
  1327.     // ----- Check the path length
  1328.     if (strlen($v_stored_filename) > 0xFF)
  1329.     {
  1330.       // ----- Error log
  1331.       $this->_errorLog(-5, "Stored file name is too long (max. 255) : '$v_stored_filename'");
  1332.  
  1333.       // ----- Return
  1334.       return Archive_Zip::errorCode();
  1335.     }
  1336.     */
  1337.  
  1338.     // ----- Set the file properties
  1339.     clearstatcache();
  1340.     $p_header['version'] = 20;
  1341.     $p_header['version_extracted'] = 10;
  1342.     $p_header['flag'] = 0;
  1343.     $p_header['compression'] = 0;
  1344.     $p_header['mtime'] = filemtime($p_filename);
  1345.     $p_header['crc'] = 0;
  1346.     $p_header['compressed_size'] = 0;
  1347.     $p_header['size'] = filesize($p_filename);
  1348.     $p_header['filename_len'] = strlen($p_filename);
  1349.     $p_header['extra_len'] = 0;
  1350.     $p_header['comment_len'] = 0;
  1351.     $p_header['disk'] = 0;
  1352.     $p_header['internal'] = 0;
  1353.     $p_header['external'] = (is_file($p_filename)?0xFE49FFE0:0x41FF0010);
  1354.     $p_header['offset'] = 0;
  1355.     $p_header['filename'] = $p_filename;
  1356.     $p_header['stored_filename'] = $v_stored_filename;
  1357.     $p_header['extra'] = '';
  1358.     $p_header['comment'] = '';
  1359.     $p_header['status'] = 'ok';
  1360.     $p_header['index'] = -1;
  1361.  
  1362.     // ----- Look for pre-add callback
  1363.     if (   (isset($p_params[ARCHIVE_ZIP_PARAM_PRE_ADD]))
  1364.         && ($p_params[ARCHIVE_ZIP_PARAM_PRE_ADD] != '')) {
  1365.  
  1366.       // ----- Generate a local information
  1367.       $v_local_header = array();
  1368.       $this->_convertHeader2FileInfo($p_header, $v_local_header);
  1369.  
  1370.       // ----- Call the callback
  1371.       // Here I do not use call_user_func() because I need to send a reference to the
  1372.       // header.
  1373.       eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_PRE_ADD].'(ARCHIVE_ZIP_PARAM_PRE_ADD, $v_local_header);');
  1374.       if ($v_result == 0) {
  1375.         // ----- Change the file status
  1376.         $p_header['status'] = "skipped";
  1377.         $v_result = 1;
  1378.       }
  1379.  
  1380.       // ----- Update the informations
  1381.       // Only some fields can be modified
  1382.       if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  1383.         $p_header['stored_filename'] = $this->_tool_PathReduction($v_local_header['stored_filename']);
  1384.       }
  1385.     }
  1386.  
  1387.     // ----- Look for empty stored filename
  1388.     if ($p_header['stored_filename'] == "") {
  1389.       $p_header['status'] = "filtered";
  1390.     }
  1391.  
  1392.     // ----- Check the path length
  1393.     if (strlen($p_header['stored_filename']) > 0xFF) {
  1394.       $p_header['status'] = 'filename_too_long';
  1395.     }
  1396.  
  1397.     // ----- Look if no error, or file not skipped
  1398.     if ($p_header['status'] == 'ok') {
  1399.  
  1400.       // ----- Look for a file
  1401.       if (is_file($p_filename))
  1402.       {
  1403.         // ----- Open the source file
  1404.         if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1405.           $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  1406.           return Archive_Zip::errorCode();
  1407.         }
  1408.         
  1409.         if ($p_params['no_compression']) {
  1410.           // ----- Read the file content
  1411.           $v_content_compressed = @fread($v_file, $p_header['size']);
  1412.  
  1413.           // ----- Calculate the CRC
  1414.           $p_header['crc'] = crc32($v_content_compressed);
  1415.         }
  1416.         else {
  1417.           // ----- Read the file content
  1418.           $v_content = @fread($v_file, $p_header['size']);
  1419.  
  1420.           // ----- Calculate the CRC
  1421.           $p_header['crc'] = crc32($v_content);
  1422.  
  1423.           // ----- Compress the file
  1424.           $v_content_compressed = gzdeflate($v_content);
  1425.         }
  1426.  
  1427.         // ----- Set header parameters
  1428.         $p_header['compressed_size'] = strlen($v_content_compressed);
  1429.         $p_header['compression'] = 8;
  1430.  
  1431.         // ----- Call the header generation
  1432.         if (($v_result = $this->_writeFileHeader($p_header)) != 1) {
  1433.           @fclose($v_file);
  1434.           return $v_result;
  1435.         }
  1436.  
  1437.         // ----- Write the compressed content
  1438.         $v_binary_data = pack('a'.$p_header['compressed_size'], $v_content_compressed);
  1439.         @fwrite($this->_zip_fd, $v_binary_data, $p_header['compressed_size']);
  1440.  
  1441.         // ----- Close the file
  1442.         @fclose($v_file);
  1443.       }
  1444.  
  1445.       // ----- Look for a directory
  1446.       else
  1447.       {
  1448.         // ----- Set the file properties
  1449.         $p_header['filename'] .= '/';
  1450.         $p_header['filename_len']++;
  1451.         $p_header['size'] = 0;
  1452.         $p_header['external'] = 0x41FF0010;   // Value for a folder : to be checked
  1453.  
  1454.         // ----- Call the header generation
  1455.         if (($v_result = $this->_writeFileHeader($p_header)) != 1)
  1456.         {
  1457.           return $v_result;
  1458.         }
  1459.       }
  1460.     }
  1461.  
  1462.     // ----- Look for pre-add callback
  1463.     if (   (isset($p_params[ARCHIVE_ZIP_PARAM_POST_ADD]))
  1464.         && ($p_params[ARCHIVE_ZIP_PARAM_POST_ADD] != '')) {
  1465.  
  1466.       // ----- Generate a local information
  1467.       $v_local_header = array();
  1468.       $this->_convertHeader2FileInfo($p_header, $v_local_header);
  1469.  
  1470.       // ----- Call the callback
  1471.       // Here I do not use call_user_func() because I need to send a reference to the
  1472.       // header.
  1473.       eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_POST_ADD].'(ARCHIVE_ZIP_PARAM_POST_ADD, $v_local_header);');
  1474.       if ($v_result == 0) {
  1475.         // ----- Ignored
  1476.         $v_result = 1;
  1477.       }
  1478.  
  1479.       // ----- Update the informations
  1480.       // Nothing can be modified
  1481.     }
  1482.  
  1483.     // ----- Return
  1484.     return $v_result;
  1485.   }
  1486.   // ---------------------------------------------------------------------------
  1487.  
  1488.   // ---------------------------------------------------------------------------
  1489.   // Function : _writeFileHeader()
  1490.   // Description :
  1491.   // Parameters :
  1492.   // Return Values :
  1493.   // ---------------------------------------------------------------------------
  1494.   /**
  1495.   * Archive_Zip::_writeFileHeader()
  1496.   *
  1497.   * { Description }
  1498.   *
  1499.   */
  1500.   function _writeFileHeader(&$p_header)
  1501.   {
  1502.     $v_result=1;
  1503.  
  1504.     // TBC
  1505.     //for(reset($p_header); $key = key($p_header); next($p_header)) {
  1506.     //}
  1507.  
  1508.     // ----- Store the offset position of the file
  1509.     $p_header['offset'] = ftell($this->_zip_fd);
  1510.  
  1511.     // ----- Transform UNIX mtime to DOS format mdate/mtime
  1512.     $v_date = getdate($p_header['mtime']);
  1513.     $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  1514.     $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  1515.  
  1516.     // ----- Packed data
  1517.     $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50, $p_header['version'], $p_header['flag'],
  1518.                           $p_header['compression'], $v_mtime, $v_mdate,
  1519.                           $p_header['crc'], $p_header['compressed_size'], $p_header['size'],
  1520.                           strlen($p_header['stored_filename']), $p_header['extra_len']);
  1521.  
  1522.     // ----- Write the first 148 bytes of the header in the archive
  1523.     fputs($this->_zip_fd, $v_binary_data, 30);
  1524.  
  1525.     // ----- Write the variable fields
  1526.     if (strlen($p_header['stored_filename']) != 0)
  1527.     {
  1528.       fputs($this->_zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1529.     }
  1530.     if ($p_header['extra_len'] != 0)
  1531.     {
  1532.       fputs($this->_zip_fd, $p_header['extra'], $p_header['extra_len']);
  1533.     }
  1534.  
  1535.     // ----- Return
  1536.     return $v_result;
  1537.   }
  1538.   // ---------------------------------------------------------------------------
  1539.  
  1540.   // ---------------------------------------------------------------------------
  1541.   // Function : _writeCentralFileHeader()
  1542.   // Description :
  1543.   // Parameters :
  1544.   // Return Values :
  1545.   // ---------------------------------------------------------------------------
  1546.   /**
  1547.   * Archive_Zip::_writeCentralFileHeader()
  1548.   *
  1549.   * { Description }
  1550.   *
  1551.   */
  1552.   function _writeCentralFileHeader(&$p_header)
  1553.   {
  1554.     $v_result=1;
  1555.  
  1556.     // TBC
  1557.     //for(reset($p_header); $key = key($p_header); next($p_header)) {
  1558.     //}
  1559.  
  1560.     // ----- Transform UNIX mtime to DOS format mdate/mtime
  1561.     $v_date = getdate($p_header['mtime']);
  1562.     $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  1563.     $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  1564.  
  1565.     // ----- Packed data
  1566.     $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50, $p_header['version'], $p_header['version_extracted'],
  1567.                           $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'],
  1568.                           $p_header['compressed_size'], $p_header['size'],
  1569.                           strlen($p_header['stored_filename']), $p_header['extra_len'], $p_header['comment_len'],
  1570.                           $p_header['disk'], $p_header['internal'], $p_header['external'], $p_header['offset']);
  1571.  
  1572.     // ----- Write the 42 bytes of the header in the zip file
  1573.     fputs($this->_zip_fd, $v_binary_data, 46);
  1574.  
  1575.     // ----- Write the variable fields
  1576.     if (strlen($p_header['stored_filename']) != 0)
  1577.     {
  1578.       fputs($this->_zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1579.     }
  1580.     if ($p_header['extra_len'] != 0)
  1581.     {
  1582.       fputs($this->_zip_fd, $p_header['extra'], $p_header['extra_len']);
  1583.     }
  1584.     if ($p_header['comment_len'] != 0)
  1585.     {
  1586.       fputs($this->_zip_fd, $p_header['comment'], $p_header['comment_len']);
  1587.     }
  1588.  
  1589.     // ----- Return
  1590.     return $v_result;
  1591.   }
  1592.   // ---------------------------------------------------------------------------
  1593.  
  1594.   // ---------------------------------------------------------------------------
  1595.   // Function : _writeCentralHeader()
  1596.   // Description :
  1597.   // Parameters :
  1598.   // Return Values :
  1599.   // ---------------------------------------------------------------------------
  1600.   /**
  1601.   * Archive_Zip::_writeCentralHeader()
  1602.   *
  1603.   * { Description }
  1604.   *
  1605.   */
  1606.   function _writeCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  1607.   {
  1608.     $v_result=1;
  1609.  
  1610.     // ----- Packed data
  1611.     $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries, $p_nb_entries, $p_size, $p_offset, strlen($p_comment));
  1612.  
  1613.     // ----- Write the 22 bytes of the header in the zip file
  1614.     fputs($this->_zip_fd, $v_binary_data, 22);
  1615.  
  1616.     // ----- Write the variable fields
  1617.     if (strlen($p_comment) != 0)
  1618.     {
  1619.       fputs($this->_zip_fd, $p_comment, strlen($p_comment));
  1620.     }
  1621.  
  1622.     // ----- Return
  1623.     return $v_result;
  1624.   }
  1625.   // ---------------------------------------------------------------------------
  1626.  
  1627.   // ---------------------------------------------------------------------------
  1628.   // Function : _list()
  1629.   // Description :
  1630.   // Parameters :
  1631.   // Return Values :
  1632.   // ---------------------------------------------------------------------------
  1633.   /**
  1634.   * Archive_Zip::_list()
  1635.   *
  1636.   * { Description }
  1637.   *
  1638.   */
  1639.   function _list(&$p_list)
  1640.   {
  1641.     $v_result=1;
  1642.  
  1643.     // ----- Open the zip file
  1644.     if (($this->_zip_fd = @fopen($this->_zipname, 'rb')) == 0)
  1645.     {
  1646.       // ----- Error log
  1647.       $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->_zipname.'\' in binary read mode');
  1648.  
  1649.       // ----- Return
  1650.       return Archive_Zip::errorCode();
  1651.     }
  1652.  
  1653.     // ----- Read the central directory informations
  1654.     $v_central_dir = array();
  1655.     if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1)
  1656.     {
  1657.       return $v_result;
  1658.     }
  1659.  
  1660.     // ----- Go to beginning of Central Dir
  1661.     @rewind($this->_zip_fd);
  1662.     if (@fseek($this->_zip_fd, $v_central_dir['offset']))
  1663.     {
  1664.       // ----- Error log
  1665.       $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1666.  
  1667.       // ----- Return
  1668.       return Archive_Zip::errorCode();
  1669.     }
  1670.  
  1671.     // ----- Read each entry
  1672.     for ($i=0; $i<$v_central_dir['entries']; $i++)
  1673.     {
  1674.       // ----- Read the file header
  1675.       if (($v_result = $this->_readCentralFileHeader($v_header)) != 1)
  1676.       {
  1677.         return $v_result;
  1678.       }
  1679.       $v_header['index'] = $i;
  1680.  
  1681.       // ----- Get the only interesting attributes
  1682.       $this->_convertHeader2FileInfo($v_header, $p_list[$i]);
  1683.       unset($v_header);
  1684.     }
  1685.  
  1686.     // ----- Close the zip file
  1687.     $this->_closeFd();
  1688.  
  1689.     // ----- Return
  1690.     return $v_result;
  1691.   }
  1692.   // ---------------------------------------------------------------------------
  1693.  
  1694.   // ---------------------------------------------------------------------------
  1695.   // Function : _convertHeader2FileInfo()
  1696.   // Description :
  1697.   //   This function takes the file informations from the central directory
  1698.   //   entries and extract the interesting parameters that will be given back.
  1699.   //   The resulting file infos are set in the array $p_info
  1700.   //     $p_info['filename'] : Filename with full path. Given by user (add),
  1701.   //                           extracted in the filesystem (extract).
  1702.   //     $p_info['stored_filename'] : Stored filename in the archive.
  1703.   //     $p_info['size'] = Size of the file.
  1704.   //     $p_info['compressed_size'] = Compressed size of the file.
  1705.   //     $p_info['mtime'] = Last modification date of the file.
  1706.   //     $p_info['comment'] = Comment associated with the file.
  1707.   //     $p_info['folder'] = true/false : indicates if the entry is a folder or not.
  1708.   //     $p_info['status'] = status of the action on the file.
  1709.   // Parameters :
  1710.   // Return Values :
  1711.   // ---------------------------------------------------------------------------
  1712.   /**
  1713.   * Archive_Zip::_convertHeader2FileInfo()
  1714.   *
  1715.   * { Description }
  1716.   *
  1717.   */
  1718.   function _convertHeader2FileInfo($p_header, &$p_info)
  1719.   {
  1720.     $v_result=1;
  1721.  
  1722.     // ----- Get the interesting attributes
  1723.     $p_info['filename'] = $p_header['filename'];
  1724.     $p_info['stored_filename'] = $p_header['stored_filename'];
  1725.     $p_info['size'] = $p_header['size'];
  1726.     $p_info['compressed_size'] = $p_header['compressed_size'];
  1727.     $p_info['mtime'] = $p_header['mtime'];
  1728.     $p_info['comment'] = $p_header['comment'];
  1729.     $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
  1730.     $p_info['index'] = $p_header['index'];
  1731.     $p_info['status'] = $p_header['status'];
  1732.  
  1733.     // ----- Return
  1734.     return $v_result;
  1735.   }
  1736.   // ---------------------------------------------------------------------------
  1737.  
  1738.   // ---------------------------------------------------------------------------
  1739.   // Function : _extractByRule()
  1740.   // Description :
  1741.   //   Extract a file or directory depending of rules (by index, by name, ...)
  1742.   // Parameters :
  1743.   //   $p_file_list : An array where will be placed the properties of each
  1744.   //                  extracted file
  1745.   //   $p_path : Path to add while writing the extracted files
  1746.   //   $p_remove_path : Path to remove (from the file memorized path) while writing the
  1747.   //                    extracted files. If the path does not match the file path,
  1748.   //                    the file is extracted with its memorized path.
  1749.   //                    $p_remove_path does not apply to 'list' mode.
  1750.   //                    $p_path and $p_remove_path are commulative.
  1751.   // Return Values :
  1752.   //   1 on success,0 or less on error (see error code list)
  1753.   // ---------------------------------------------------------------------------
  1754.   /**
  1755.   * Archive_Zip::_extractByRule()
  1756.   *
  1757.   * { Description }
  1758.   *
  1759.   */
  1760.   function _extractByRule(&$p_file_list, &$p_params)
  1761.   {
  1762.     $v_result=1;
  1763.  
  1764.     $p_path = $p_params['add_path'];
  1765.     $p_remove_path = $p_params['remove_path'];
  1766.     $p_remove_all_path = $p_params['remove_all_path'];
  1767.  
  1768.     // ----- Check the path
  1769.     if (($p_path == "")
  1770.         || ((substr($p_path, 0, 1) != "/")
  1771.         && (substr($p_path, 0, 3) != "../") && (substr($p_path,1,2)!=":/")))
  1772.       $p_path = "./".$p_path;
  1773.  
  1774.     // ----- Reduce the path last (and duplicated) '/'
  1775.     if (($p_path != "./") && ($p_path != "/")) {
  1776.       // ----- Look for the path end '/'
  1777.       while (substr($p_path, -1) == "/") {
  1778.         $p_path = substr($p_path, 0, strlen($p_path)-1);
  1779.       }
  1780.     }
  1781.  
  1782.     // ----- Look for path to remove format (should end by /)
  1783.     if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/')) {
  1784.       $p_remove_path .= '/';
  1785.     }
  1786.     $p_remove_path_size = strlen($p_remove_path);
  1787.  
  1788.     // ----- Open the zip file
  1789.     if (($v_result = $this->_openFd('rb')) != 1)
  1790.     {
  1791.       return $v_result;
  1792.     }
  1793.  
  1794.     // ----- Read the central directory informations
  1795.     $v_central_dir = array();
  1796.     if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1)
  1797.     {
  1798.       // ----- Close the zip file
  1799.       $this->_closeFd();
  1800.  
  1801.       return $v_result;
  1802.     }
  1803.  
  1804.     // ----- Start at beginning of Central Dir
  1805.     $v_pos_entry = $v_central_dir['offset'];
  1806.  
  1807.     // ----- Read each entry
  1808.     $j_start = 0;
  1809.     for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++) {
  1810.       // ----- Read next Central dir entry
  1811.       @rewind($this->_zip_fd);
  1812.       if (@fseek($this->_zip_fd, $v_pos_entry)) {
  1813.         $this->_closeFd();
  1814.  
  1815.         $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
  1816.                          'Invalid archive size');
  1817.  
  1818.         return Archive_Zip::errorCode();
  1819.       }
  1820.  
  1821.       // ----- Read the file header
  1822.       $v_header = array();
  1823.       if (($v_result = $this->_readCentralFileHeader($v_header)) != 1) {
  1824.         $this->_closeFd();
  1825.  
  1826.         return $v_result;
  1827.       }
  1828.  
  1829.       // ----- Store the index
  1830.       $v_header['index'] = $i;
  1831.  
  1832.       // ----- Store the file position
  1833.       $v_pos_entry = ftell($this->_zip_fd);
  1834.  
  1835.       // ----- Look for the specific extract rules
  1836.       $v_extract = false;
  1837.  
  1838.       // ----- Look for extract by name rule
  1839.       if (   (isset($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
  1840.           && ($p_params[ARCHIVE_ZIP_PARAM_BY_NAME] != 0)) {
  1841.  
  1842.           // ----- Look if the filename is in the list
  1843.           for ($j=0;
  1844.                   ($j<sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
  1845.                && (!$v_extract);
  1846.                $j++) {
  1847.  
  1848.               // ----- Look for a directory
  1849.               if (substr($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j], -1) == "/") {
  1850.  
  1851.                   // ----- Look if the directory is in the filename path
  1852.                   if (   (strlen($v_header['stored_filename']) > strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]))
  1853.                       && (substr($v_header['stored_filename'], 0, strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) {
  1854.                       $v_extract = true;
  1855.                   }
  1856.               }
  1857.               // ----- Look for a filename
  1858.               elseif ($v_header['stored_filename'] == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) {
  1859.                   $v_extract = true;
  1860.               }
  1861.           }
  1862.       }
  1863.  
  1864.       // ----- Look for extract by ereg rule
  1865.       else if (   (isset($p_params[ARCHIVE_ZIP_PARAM_BY_EREG]))
  1866.                && ($p_params[ARCHIVE_ZIP_PARAM_BY_EREG] != "")) {
  1867.  
  1868.           if (ereg($p_params[ARCHIVE_ZIP_PARAM_BY_EREG], $v_header['stored_filename'])) {
  1869.               $v_extract = true;
  1870.           }
  1871.       }
  1872.  
  1873.       // ----- Look for extract by preg rule
  1874.       else if (   (isset($p_params[ARCHIVE_ZIP_PARAM_BY_PREG]))
  1875.                && ($p_params[ARCHIVE_ZIP_PARAM_BY_PREG] != "")) {
  1876.  
  1877.           if (preg_match($p_params[ARCHIVE_ZIP_PARAM_BY_PREG], $v_header['stored_filename'])) {
  1878.               $v_extract = true;
  1879.           }
  1880.       }
  1881.  
  1882.       // ----- Look for extract by index rule
  1883.       else if (   (isset($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]))
  1884.                && ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX] != 0)) {
  1885.  
  1886.           // ----- Look if the index is in the list
  1887.           for ($j=$j_start; ($j<sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX])) && (!$v_extract); $j++) {
  1888.  
  1889.               if (($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start']) && ($i<=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end'])) {
  1890.                   $v_extract = true;
  1891.               }
  1892.               if ($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end']) {
  1893.                   $j_start = $j+1;
  1894.               }
  1895.  
  1896.               if ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start']>$i) {
  1897.                   break;
  1898.               }
  1899.           }
  1900.       }
  1901.  
  1902.       // ----- Look for no rule, which means extract all the archive
  1903.       else {
  1904.           $v_extract = true;
  1905.       }
  1906.  
  1907.  
  1908.       // ----- Look for real extraction
  1909.       if ($v_extract)
  1910.       {
  1911.  
  1912.         // ----- Go to the file position
  1913.         @rewind($this->_zip_fd);
  1914.         if (@fseek($this->_zip_fd, $v_header['offset']))
  1915.         {
  1916.           // ----- Close the zip file
  1917.           $this->_closeFd();
  1918.  
  1919.           // ----- Error log
  1920.           $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1921.  
  1922.           // ----- Return
  1923.           return Archive_Zip::errorCode();
  1924.         }
  1925.  
  1926.         // ----- Look for extraction as string
  1927.         if ($p_params[ARCHIVE_ZIP_PARAM_EXTRACT_AS_STRING]) {
  1928.  
  1929.           // ----- Extracting the file
  1930.           if (($v_result = $this->_extractFileAsString($v_header, $v_string)) != 1)
  1931.           {
  1932.             // ----- Close the zip file
  1933.             $this->_closeFd();
  1934.  
  1935.             return $v_result;
  1936.           }
  1937.  
  1938.           // ----- Get the only interesting attributes
  1939.           if (($v_result = $this->_convertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
  1940.           {
  1941.             // ----- Close the zip file
  1942.             $this->_closeFd();
  1943.  
  1944.             return $v_result;
  1945.           }
  1946.  
  1947.           // ----- Set the file content
  1948.           $p_file_list[$v_nb_extracted]['content'] = $v_string;
  1949.  
  1950.           // ----- Next extracted file
  1951.           $v_nb_extracted++;
  1952.         }
  1953.         else {
  1954.           // ----- Extracting the file
  1955.           if (($v_result = $this->_extractFile($v_header, $p_path, $p_remove_path, $p_remove_all_path, $p_params)) != 1)
  1956.           {
  1957.             // ----- Close the zip file
  1958.             $this->_closeFd();
  1959.  
  1960.             return $v_result;
  1961.           }
  1962.  
  1963.           // ----- Get the only interesting attributes
  1964.           if (($v_result = $this->_convertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
  1965.           {
  1966.             // ----- Close the zip file
  1967.             $this->_closeFd();
  1968.  
  1969.             return $v_result;
  1970.           }
  1971.         }
  1972.       }
  1973.     }
  1974.  
  1975.     // ----- Close the zip file
  1976.     $this->_closeFd();
  1977.  
  1978.     // ----- Return
  1979.     return $v_result;
  1980.   }
  1981.   // ---------------------------------------------------------------------------
  1982.  
  1983.   // ---------------------------------------------------------------------------
  1984.   // Function : _extractFile()
  1985.   // Description :
  1986.   // Parameters :
  1987.   // Return Values :
  1988.   // ---------------------------------------------------------------------------
  1989.   /**
  1990.   * Archive_Zip::_extractFile()
  1991.   *
  1992.   * { Description }
  1993.   *
  1994.   */
  1995.   function _extractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_params)
  1996.   {
  1997.     $v_result=1;
  1998.  
  1999.     // ----- Read the file header
  2000.     if (($v_result = $this->_readFileHeader($v_header)) != 1)
  2001.     {
  2002.       // ----- Return
  2003.       return $v_result;
  2004.     }
  2005.  
  2006.  
  2007.     // ----- Check that the file header is coherent with $p_entry info
  2008.     // TBC
  2009.  
  2010.     // ----- Look for all path to remove
  2011.     if ($p_remove_all_path == true) {
  2012.         // ----- Get the basename of the path
  2013.         $p_entry['filename'] = basename($p_entry['filename']);
  2014.     }
  2015.  
  2016.     // ----- Look for path to remove
  2017.     else if ($p_remove_path != "")
  2018.     {
  2019.       //if (strcmp($p_remove_path, $p_entry['filename'])==0)
  2020.       if ($this->_tool_PathInclusion($p_remove_path, $p_entry['filename']) == 2)
  2021.       {
  2022.  
  2023.         // ----- Change the file status
  2024.         $p_entry['status'] = "filtered";
  2025.  
  2026.         // ----- Return
  2027.         return $v_result;
  2028.       }
  2029.  
  2030.       $p_remove_path_size = strlen($p_remove_path);
  2031.       if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
  2032.       {
  2033.  
  2034.         // ----- Remove the path
  2035.         $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  2036.  
  2037.       }
  2038.     }
  2039.  
  2040.     // ----- Add the path
  2041.     if ($p_path != '')
  2042.     {
  2043.       $p_entry['filename'] = $p_path."/".$p_entry['filename'];
  2044.     }
  2045.  
  2046.     // ----- Look for pre-extract callback
  2047.     if (   (isset($p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT]))
  2048.         && ($p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT] != '')) {
  2049.  
  2050.       // ----- Generate a local information
  2051.       $v_local_header = array();
  2052.       $this->_convertHeader2FileInfo($p_entry, $v_local_header);
  2053.  
  2054.       // ----- Call the callback
  2055.       // Here I do not use call_user_func() because I need to send a reference to the
  2056.       // header.
  2057.       eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT].'(ARCHIVE_ZIP_PARAM_PRE_EXTRACT, $v_local_header);');
  2058.       if ($v_result == 0) {
  2059.         // ----- Change the file status
  2060.         $p_entry['status'] = "skipped";
  2061.         $v_result = 1;
  2062.       }
  2063.  
  2064.       // ----- Update the informations
  2065.       // Only some fields can be modified
  2066.       $p_entry['filename'] = $v_local_header['filename'];
  2067.     }
  2068.  
  2069.     // ----- Trace
  2070.  
  2071.     // ----- Look if extraction should be done
  2072.     if ($p_entry['status'] == 'ok') {
  2073.  
  2074.     // ----- Look for specific actions while the file exist
  2075.     if (file_exists($p_entry['filename']))
  2076.     {
  2077.  
  2078.       // ----- Look if file is a directory
  2079.       if (is_dir($p_entry['filename']))
  2080.       {
  2081.  
  2082.         // ----- Change the file status
  2083.         $p_entry['status'] = "already_a_directory";
  2084.  
  2085.         // ----- Return
  2086.         //return $v_result;
  2087.       }
  2088.       // ----- Look if file is write protected
  2089.       else if (!is_writeable($p_entry['filename']))
  2090.       {
  2091.  
  2092.         // ----- Change the file status
  2093.         $p_entry['status'] = "write_protected";
  2094.  
  2095.         // ----- Return
  2096.         //return $v_result;
  2097.       }
  2098.  
  2099.       // ----- Look if the extracted file is older
  2100.       else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
  2101.       {
  2102.  
  2103.         // ----- Change the file status
  2104.         $p_entry['status'] = "newer_exist";
  2105.  
  2106.         // ----- Return
  2107.         //return $v_result;
  2108.       }
  2109.     }
  2110.  
  2111.     // ----- Check the directory availability and create it if necessary
  2112.     else {
  2113.       if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
  2114.         $v_dir_to_check = $p_entry['filename'];
  2115.       else if (!strstr($p_entry['filename'], "/"))
  2116.         $v_dir_to_check = "";
  2117.       else
  2118.         $v_dir_to_check = dirname($p_entry['filename']);
  2119.  
  2120.       if (($v_result = $this->_dirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
  2121.  
  2122.         // ----- Change the file status
  2123.         $p_entry['status'] = "path_creation_fail";
  2124.  
  2125.         // ----- Return
  2126.         //return $v_result;
  2127.         $v_result = 1;
  2128.       }
  2129.     }
  2130.     }
  2131.  
  2132.     // ----- Look if extraction should be done
  2133.     if ($p_entry['status'] == 'ok') {
  2134.  
  2135.       // ----- Do the extraction (if not a folder)
  2136.       if (!(($p_entry['external']&0x00000010)==0x00000010))
  2137.       {
  2138.  
  2139.         // ----- Look for not compressed file
  2140.         if ($p_entry['compressed_size'] == $p_entry['size'])
  2141.         {
  2142.  
  2143.           // ----- Opening destination file
  2144.           if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
  2145.           {
  2146.  
  2147.             // ----- Change the file status
  2148.             $p_entry['status'] = "write_error";
  2149.  
  2150.             // ----- Return
  2151.             return $v_result;
  2152.           }
  2153.  
  2154.  
  2155.           // ----- Read the file by ARCHIVE_ZIP_READ_BLOCK_SIZE octets blocks
  2156.           $v_size = $p_entry['compressed_size'];
  2157.           while ($v_size != 0)
  2158.           {
  2159.             $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2160.             $v_buffer = fread($this->_zip_fd, $v_read_size);
  2161.             $v_binary_data = pack('a'.$v_read_size, $v_buffer);
  2162.             @fwrite($v_dest_file, $v_binary_data, $v_read_size);
  2163.             $v_size -= $v_read_size;
  2164.           }
  2165.  
  2166.           // ----- Closing the destination file
  2167.           fclose($v_dest_file);
  2168.  
  2169.           // ----- Change the file mtime
  2170.           touch($p_entry['filename'], $p_entry['mtime']);
  2171.         }
  2172.         else
  2173.         {
  2174.           // ----- Trace
  2175.  
  2176.           // ----- Opening destination file
  2177.           if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  2178.  
  2179.             // ----- Change the file status
  2180.             $p_entry['status'] = "write_error";
  2181.  
  2182.             return $v_result;
  2183.           }
  2184.  
  2185.  
  2186.           // ----- Read the compressed file in a buffer (one shot)
  2187.           $v_buffer = @fread($this->_zip_fd, $p_entry['compressed_size']);
  2188.  
  2189.           // ----- Decompress the file
  2190.           $v_file_content = gzinflate($v_buffer);
  2191.           unset($v_buffer);
  2192.  
  2193.           // ----- Write the uncompressed data
  2194.           @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  2195.           unset($v_file_content);
  2196.  
  2197.           // ----- Closing the destination file
  2198.           @fclose($v_dest_file);
  2199.  
  2200.           // ----- Change the file mtime
  2201.           touch($p_entry['filename'], $p_entry['mtime']);
  2202.         }
  2203.  
  2204.         // ----- Look for chmod option
  2205.         if (   (isset($p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD]))
  2206.             && ($p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD] != 0)) {
  2207.  
  2208.           // ----- Change the mode of the file
  2209.           chmod($p_entry['filename'], $p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD]);
  2210.         }
  2211.  
  2212.       }
  2213.     }
  2214.  
  2215.     // ----- Look for post-extract callback
  2216.     if (   (isset($p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT]))
  2217.         && ($p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT] != '')) {
  2218.  
  2219.       // ----- Generate a local information
  2220.       $v_local_header = array();
  2221.       $this->_convertHeader2FileInfo($p_entry, $v_local_header);
  2222.  
  2223.       // ----- Call the callback
  2224.       // Here I do not use call_user_func() because I need to send a reference to the
  2225.       // header.
  2226.       eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT].'(ARCHIVE_ZIP_PARAM_POST_EXTRACT, $v_local_header);');
  2227.     }
  2228.  
  2229.     // ----- Return
  2230.     return $v_result;
  2231.   }
  2232.   // ---------------------------------------------------------------------------
  2233.  
  2234.   // ---------------------------------------------------------------------------
  2235.   // Function : _extractFileAsString()
  2236.   // Description :
  2237.   // Parameters :
  2238.   // Return Values :
  2239.   // ---------------------------------------------------------------------------
  2240.   /**
  2241.   * Archive_Zip::_extractFileAsString()
  2242.   *
  2243.   * { Description }
  2244.   *
  2245.   */
  2246.   function _extractFileAsString(&$p_entry, &$p_string)
  2247.   {
  2248.     $v_result=1;
  2249.  
  2250.     // ----- Read the file header
  2251.     $v_header = array();
  2252.     if (($v_result = $this->_readFileHeader($v_header)) != 1)
  2253.     {
  2254.       // ----- Return
  2255.       return $v_result;
  2256.     }
  2257.  
  2258.  
  2259.     // ----- Check that the file header is coherent with $p_entry info
  2260.     // TBC
  2261.  
  2262.     // ----- Trace
  2263.  
  2264.     // ----- Do the extraction (if not a folder)
  2265.     if (!(($p_entry['external']&0x00000010)==0x00000010))
  2266.     {
  2267.       // ----- Look for not compressed file
  2268.       if ($p_entry['compressed_size'] == $p_entry['size'])
  2269.       {
  2270.         // ----- Trace
  2271.  
  2272.         // ----- Reading the file
  2273.         $p_string = fread($this->_zip_fd, $p_entry['compressed_size']);
  2274.       }
  2275.       else
  2276.       {
  2277.         // ----- Trace
  2278.  
  2279.         // ----- Reading the file
  2280.         $v_data = fread($this->_zip_fd, $p_entry['compressed_size']);
  2281.  
  2282.         // ----- Decompress the file
  2283.         $p_string = gzinflate($v_data);
  2284.       }
  2285.  
  2286.       // ----- Trace
  2287.     }
  2288.     else {
  2289.         // TBC : error : can not extract a folder in a string
  2290.     }
  2291.  
  2292.     // ----- Return
  2293.     return $v_result;
  2294.   }
  2295.   // ---------------------------------------------------------------------------
  2296.  
  2297.   // ---------------------------------------------------------------------------
  2298.   // Function : _readFileHeader()
  2299.   // Description :
  2300.   // Parameters :
  2301.   // Return Values :
  2302.   // ---------------------------------------------------------------------------
  2303.   /**
  2304.   * Archive_Zip::_readFileHeader()
  2305.   *
  2306.   * { Description }
  2307.   *
  2308.   */
  2309.   function _readFileHeader(&$p_header)
  2310.   {
  2311.     $v_result=1;
  2312.  
  2313.     // ----- Read the 4 bytes signature
  2314.     $v_binary_data = @fread($this->_zip_fd, 4);
  2315.     $v_data = unpack('Vid', $v_binary_data);
  2316.  
  2317.     // ----- Check signature
  2318.     if ($v_data['id'] != 0x04034b50)
  2319.     {
  2320.  
  2321.       // ----- Error log
  2322.       $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  2323.  
  2324.       // ----- Return
  2325.       return Archive_Zip::errorCode();
  2326.     }
  2327.  
  2328.     // ----- Read the first 42 bytes of the header
  2329.     $v_binary_data = fread($this->_zip_fd, 26);
  2330.  
  2331.     // ----- Look for invalid block size
  2332.     if (strlen($v_binary_data) != 26)
  2333.     {
  2334.       $p_header['filename'] = "";
  2335.       $p_header['status'] = "invalid_header";
  2336.  
  2337.       // ----- Error log
  2338.       $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  2339.  
  2340.       // ----- Return
  2341.       return Archive_Zip::errorCode();
  2342.     }
  2343.  
  2344.     // ----- Extract the values
  2345.     $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  2346.  
  2347.     // ----- Get filename
  2348.     $p_header['filename'] = fread($this->_zip_fd, $v_data['filename_len']);
  2349.  
  2350.     // ----- Get extra_fields
  2351.     if ($v_data['extra_len'] != 0) {
  2352.       $p_header['extra'] = fread($this->_zip_fd, $v_data['extra_len']);
  2353.     }
  2354.     else {
  2355.       $p_header['extra'] = '';
  2356.     }
  2357.  
  2358.     // ----- Extract properties
  2359.     $p_header['compression'] = $v_data['compression'];
  2360.     $p_header['size'] = $v_data['size'];
  2361.     $p_header['compressed_size'] = $v_data['compressed_size'];
  2362.     $p_header['crc'] = $v_data['crc'];
  2363.     $p_header['flag'] = $v_data['flag'];
  2364.  
  2365.     // ----- Recuperate date in UNIX format
  2366.     $p_header['mdate'] = $v_data['mdate'];
  2367.     $p_header['mtime'] = $v_data['mtime'];
  2368.     if ($p_header['mdate'] && $p_header['mtime'])
  2369.     {
  2370.       // ----- Extract time
  2371.       $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  2372.       $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  2373.       $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  2374.  
  2375.       // ----- Extract date
  2376.       $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  2377.       $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  2378.       $v_day = $p_header['mdate'] & 0x001F;
  2379.  
  2380.       // ----- Get UNIX date format
  2381.       $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  2382.  
  2383.     }
  2384.     else
  2385.     {
  2386.       $p_header['mtime'] = time();
  2387.     }
  2388.  
  2389.     // ----- Other informations
  2390.  
  2391.     // TBC
  2392.     //for(reset($v_data); $key = key($v_data); next($v_data)) {
  2393.     //}
  2394.  
  2395.     // ----- Set the stored filename
  2396.     $p_header['stored_filename'] = $p_header['filename'];
  2397.  
  2398.     // ----- Set the status field
  2399.     $p_header['status'] = "ok";
  2400.  
  2401.     // ----- Return
  2402.     return $v_result;
  2403.   }
  2404.   // ---------------------------------------------------------------------------
  2405.  
  2406.   // ---------------------------------------------------------------------------
  2407.   // Function : _readCentralFileHeader()
  2408.   // Description :
  2409.   // Parameters :
  2410.   // Return Values :
  2411.   // ---------------------------------------------------------------------------
  2412.   /**
  2413.   * Archive_Zip::_readCentralFileHeader()
  2414.   *
  2415.   * { Description }
  2416.   *
  2417.   */
  2418.   function _readCentralFileHeader(&$p_header)
  2419.   {
  2420.     $v_result=1;
  2421.  
  2422.     // ----- Read the 4 bytes signature
  2423.     $v_binary_data = @fread($this->_zip_fd, 4);
  2424.     $v_data = unpack('Vid', $v_binary_data);
  2425.  
  2426.     // ----- Check signature
  2427.     if ($v_data['id'] != 0x02014b50)
  2428.     {
  2429.  
  2430.       // ----- Error log
  2431.       $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  2432.  
  2433.       // ----- Return
  2434.       return Archive_Zip::errorCode();
  2435.     }
  2436.  
  2437.     // ----- Read the first 42 bytes of the header
  2438.     $v_binary_data = fread($this->_zip_fd, 42);
  2439.  
  2440.     // ----- Look for invalid block size
  2441.     if (strlen($v_binary_data) != 42)
  2442.     {
  2443.       $p_header['filename'] = "";
  2444.       $p_header['status'] = "invalid_header";
  2445.  
  2446.       // ----- Error log
  2447.       $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  2448.  
  2449.       // ----- Return
  2450.       return Archive_Zip::errorCode();
  2451.     }
  2452.  
  2453.     // ----- Extract the values
  2454.     $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);
  2455.  
  2456.     // ----- Get filename
  2457.     if ($p_header['filename_len'] != 0)
  2458.       $p_header['filename'] = fread($this->_zip_fd, $p_header['filename_len']);
  2459.     else
  2460.       $p_header['filename'] = '';
  2461.  
  2462.     // ----- Get extra
  2463.     if ($p_header['extra_len'] != 0)
  2464.       $p_header['extra'] = fread($this->_zip_fd, $p_header['extra_len']);
  2465.     else
  2466.       $p_header['extra'] = '';
  2467.  
  2468.     // ----- Get comment
  2469.     if ($p_header['comment_len'] != 0)
  2470.       $p_header['comment'] = fread($this->_zip_fd, $p_header['comment_len']);
  2471.     else
  2472.       $p_header['comment'] = '';
  2473.  
  2474.     // ----- Extract properties
  2475.  
  2476.     // ----- Recuperate date in UNIX format
  2477.     if ($p_header['mdate'] && $p_header['mtime'])
  2478.     {
  2479.       // ----- Extract time
  2480.       $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  2481.       $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  2482.       $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  2483.  
  2484.       // ----- Extract date
  2485.       $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  2486.       $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  2487.       $v_day = $p_header['mdate'] & 0x001F;
  2488.  
  2489.       // ----- Get UNIX date format
  2490.       $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  2491.  
  2492.     }
  2493.     else
  2494.     {
  2495.       $p_header['mtime'] = time();
  2496.     }
  2497.  
  2498.     // ----- Set the stored filename
  2499.     $p_header['stored_filename'] = $p_header['filename'];
  2500.  
  2501.     // ----- Set default status to ok
  2502.     $p_header['status'] = 'ok';
  2503.  
  2504.     // ----- Look if it is a directory
  2505.     if (substr($p_header['filename'], -1) == '/')
  2506.     {
  2507.       $p_header['external'] = 0x41FF0010;
  2508.     }
  2509.  
  2510.  
  2511.     // ----- Return
  2512.     return $v_result;
  2513.   }
  2514.   // ---------------------------------------------------------------------------
  2515.  
  2516.   // ---------------------------------------------------------------------------
  2517.   // Function : _readEndCentralDir()
  2518.   // Description :
  2519.   // Parameters :
  2520.   // Return Values :
  2521.   // ---------------------------------------------------------------------------
  2522.   /**
  2523.   * Archive_Zip::_readEndCentralDir()
  2524.   *
  2525.   * { Description }
  2526.   *
  2527.   */
  2528.   function _readEndCentralDir(&$p_central_dir)
  2529.   {
  2530.     $v_result=1;
  2531.  
  2532.     // ----- Go to the end of the zip file
  2533.     $v_size = filesize($this->_zipname);
  2534.     @fseek($this->_zip_fd, $v_size);
  2535.     if (@ftell($this->_zip_fd) != $v_size) {
  2536.       $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2537.                        'Unable to go to the end of the archive \''
  2538.                        .$this->_zipname.'\'');
  2539.       return Archive_Zip::errorCode();
  2540.     }
  2541.  
  2542.     // ----- First try : look if this is an archive with no commentaries
  2543.     // (most of the time)
  2544.     // in this case the end of central dir is at 22 bytes of the file end
  2545.     $v_found = 0;
  2546.     if ($v_size > 26) {
  2547.       @fseek($this->_zip_fd, $v_size-22);
  2548.       if (($v_pos = @ftell($this->_zip_fd)) != ($v_size-22)) {
  2549.         $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2550.                          'Unable to seek back to the middle of the archive \''
  2551.                          .$this->_zipname.'\'');
  2552.         return Archive_Zip::errorCode();
  2553.       }
  2554.  
  2555.       // ----- Read for bytes
  2556.       $v_binary_data = @fread($this->_zip_fd, 4);
  2557.       $v_data = unpack('Vid', $v_binary_data);
  2558.  
  2559.       // ----- Check signature
  2560.       if ($v_data['id'] == 0x06054b50) {
  2561.         $v_found = 1;
  2562.       }
  2563.  
  2564.       $v_pos = ftell($this->_zip_fd);
  2565.     }
  2566.  
  2567.     // ----- Go back to the maximum possible size of the Central Dir End Record
  2568.     if (!$v_found) {
  2569.       $v_maximum_size = 65557; // 0xFFFF + 22;
  2570.       if ($v_maximum_size > $v_size)
  2571.         $v_maximum_size = $v_size;
  2572.       @fseek($this->_zip_fd, $v_size-$v_maximum_size);
  2573.       if (@ftell($this->_zip_fd) != ($v_size-$v_maximum_size)) {
  2574.         $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2575.                          'Unable to seek back to the middle of the archive \''
  2576.                          .$this->_zipname.'\'');
  2577.         return Archive_Zip::errorCode();
  2578.       }
  2579.  
  2580.       // ----- Read byte per byte in order to find the signature
  2581.       $v_pos = ftell($this->_zip_fd);
  2582.       $v_bytes = 0x00000000;
  2583.       while ($v_pos < $v_size) {
  2584.         // ----- Read a byte
  2585.         $v_byte = @fread($this->_zip_fd, 1);
  2586.  
  2587.         // -----  Add the byte
  2588.         $v_bytes = ($v_bytes << 8) | Ord($v_byte);
  2589.  
  2590.         // ----- Compare the bytes
  2591.         if ($v_bytes == 0x504b0506) {
  2592.           $v_pos++;
  2593.           break;
  2594.         }
  2595.  
  2596.         $v_pos++;
  2597.       }
  2598.  
  2599.       // ----- Look if not found end of central dir
  2600.       if ($v_pos == $v_size) {
  2601.         $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2602.                          "Unable to find End of Central Dir Record signature");
  2603.         return Archive_Zip::errorCode();
  2604.       }
  2605.     }
  2606.  
  2607.     // ----- Read the first 18 bytes of the header
  2608.     $v_binary_data = fread($this->_zip_fd, 18);
  2609.  
  2610.     // ----- Look for invalid block size
  2611.     if (strlen($v_binary_data) != 18) {
  2612.       $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2613.                        "Invalid End of Central Dir Record size : "
  2614.                        .strlen($v_binary_data));
  2615.       return Archive_Zip::errorCode();
  2616.     }
  2617.  
  2618.     // ----- Extract the values
  2619.     $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  2620.  
  2621.     // ----- Check the global size
  2622.     if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  2623.       $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2624.                        "Fail to find the right signature");
  2625.       return Archive_Zip::errorCode();
  2626.     }
  2627.  
  2628.     // ----- Get comment
  2629.     if ($v_data['comment_size'] != 0)
  2630.       $p_central_dir['comment'] = fread($this->_zip_fd, $v_data['comment_size']);
  2631.     else
  2632.       $p_central_dir['comment'] = '';
  2633.  
  2634.     $p_central_dir['entries'] = $v_data['entries'];
  2635.     $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  2636.     $p_central_dir['offset'] = $v_data['offset'];
  2637.     $p_central_dir['size'] = $v_data['size'];
  2638.     $p_central_dir['disk'] = $v_data['disk'];
  2639.     $p_central_dir['disk_start'] = $v_data['disk_start'];
  2640.  
  2641.     // ----- Return
  2642.     return $v_result;
  2643.   }
  2644.   // ---------------------------------------------------------------------------
  2645.  
  2646.   // ---------------------------------------------------------------------------
  2647.   // Function : _deleteByRule()
  2648.   // Description :
  2649.   // Parameters :
  2650.   // Return Values :
  2651.   // ---------------------------------------------------------------------------
  2652.   /**
  2653.   * Archive_Zip::_deleteByRule()
  2654.   *
  2655.   * { Description }
  2656.   *
  2657.   */
  2658.   function _deleteByRule(&$p_result_list, &$p_params)
  2659.   {
  2660.     $v_result=1;
  2661.     $v_list_detail = array();
  2662.  
  2663.     // ----- Open the zip file
  2664.     if (($v_result=$this->_openFd('rb')) != 1)
  2665.     {
  2666.       // ----- Return
  2667.       return $v_result;
  2668.     }
  2669.  
  2670.     // ----- Read the central directory informations
  2671.     $v_central_dir = array();
  2672.     if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1)
  2673.     {
  2674.       $this->_closeFd();
  2675.       return $v_result;
  2676.     }
  2677.  
  2678.     // ----- Go to beginning of File
  2679.     @rewind($this->_zip_fd);
  2680.  
  2681.     // ----- Scan all the files
  2682.     // ----- Start at beginning of Central Dir
  2683.     $v_pos_entry = $v_central_dir['offset'];
  2684.     @rewind($this->_zip_fd);
  2685.     if (@fseek($this->_zip_fd, $v_pos_entry)) {
  2686.       // ----- Clean
  2687.       $this->_closeFd();
  2688.  
  2689.       $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
  2690.                        'Invalid archive size');
  2691.       return Archive_Zip::errorCode();
  2692.     }
  2693.  
  2694.     // ----- Read each entry
  2695.     $v_header_list = array();
  2696.     $j_start = 0;
  2697.     for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++) {
  2698.  
  2699.       // ----- Read the file header
  2700.       $v_header_list[$v_nb_extracted] = array();
  2701.       $v_result
  2702.         = $this->_readCentralFileHeader($v_header_list[$v_nb_extracted]);
  2703.       if ($v_result != 1) {
  2704.         // ----- Clean
  2705.         $this->_closeFd();
  2706.  
  2707.         return $v_result;
  2708.       }
  2709.  
  2710.       // ----- Store the index
  2711.       $v_header_list[$v_nb_extracted]['index'] = $i;
  2712.  
  2713.       // ----- Look for the specific extract rules
  2714.       $v_found = false;
  2715.  
  2716.       // ----- Look for extract by name rule
  2717.       if (   (isset($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
  2718.           && ($p_params[ARCHIVE_ZIP_PARAM_BY_NAME] != 0)) {
  2719.  
  2720.           // ----- Look if the filename is in the list
  2721.           for ($j=0;
  2722.                ($j<sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
  2723.                  && (!$v_found);
  2724.                $j++) {
  2725.  
  2726.               // ----- Look for a directory
  2727.               if (substr($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j], -1) == "/") {
  2728.  
  2729.                   // ----- Look if the directory is in the filename path
  2730.                   if (   (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]))
  2731.                       && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) {
  2732.                       $v_found = true;
  2733.                   }
  2734.                   elseif (   (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
  2735.                           && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) {
  2736.                       $v_found = true;
  2737.                   }
  2738.               }
  2739.               // ----- Look for a filename
  2740.               elseif ($v_header_list[$v_nb_extracted]['stored_filename']
  2741.                       == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) {
  2742.                   $v_found = true;
  2743.               }
  2744.           }
  2745.       }
  2746.  
  2747.       // ----- Look for extract by ereg rule
  2748.       else if (   (isset($p_params[ARCHIVE_ZIP_PARAM_BY_EREG]))
  2749.                && ($p_params[ARCHIVE_ZIP_PARAM_BY_EREG] != "")) {
  2750.  
  2751.           if (ereg($p_params[ARCHIVE_ZIP_PARAM_BY_EREG],
  2752.                    $v_header_list[$v_nb_extracted]['stored_filename'])) {
  2753.               $v_found = true;
  2754.           }
  2755.       }
  2756.  
  2757.       // ----- Look for extract by preg rule
  2758.       else if (   (isset($p_params[ARCHIVE_ZIP_PARAM_BY_PREG]))
  2759.                && ($p_params[ARCHIVE_ZIP_PARAM_BY_PREG] != "")) {
  2760.  
  2761.           if (preg_match($p_params[ARCHIVE_ZIP_PARAM_BY_PREG],
  2762.                          $v_header_list[$v_nb_extracted]['stored_filename'])) {
  2763.               $v_found = true;
  2764.           }
  2765.       }
  2766.  
  2767.       // ----- Look for extract by index rule
  2768.       else if (   (isset($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]))
  2769.                && ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX] != 0)) {
  2770.  
  2771.           // ----- Look if the index is in the list
  2772.           for ($j=$j_start;
  2773.                ($j<sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]))
  2774.                  && (!$v_found);
  2775.                $j++) {
  2776.  
  2777.               if (   ($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start'])
  2778.                   && ($i<=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end'])) {
  2779.                   $v_found = true;
  2780.               }
  2781.               if ($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end']) {
  2782.                   $j_start = $j+1;
  2783.               }
  2784.  
  2785.               if ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start']>$i) {
  2786.                   break;
  2787.               }
  2788.           }
  2789.       }
  2790.  
  2791.       // ----- Look for deletion
  2792.       if ($v_found) {
  2793.         unset($v_header_list[$v_nb_extracted]);
  2794.       }
  2795.       else {
  2796.         $v_nb_extracted++;
  2797.       }
  2798.     }
  2799.  
  2800.     // ----- Look if something need to be deleted
  2801.     if ($v_nb_extracted > 0) {
  2802.  
  2803.         // ----- Creates a temporay file
  2804.         $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR.uniqid('archive_zip-')
  2805.                            .'.tmp';
  2806.  
  2807.         // ----- Creates a temporary zip archive
  2808.         $v_temp_zip = new Archive_Zip($v_zip_temp_name);
  2809.  
  2810.         // ----- Open the temporary zip file in write mode
  2811.         if (($v_result = $v_temp_zip->_openFd('wb')) != 1) {
  2812.             $this->_closeFd();
  2813.  
  2814.             // ----- Return
  2815.             return $v_result;
  2816.         }
  2817.  
  2818.         // ----- Look which file need to be kept
  2819.         for ($i=0; $i<sizeof($v_header_list); $i++) {
  2820.  
  2821.             // ----- Calculate the position of the header
  2822.             @rewind($this->_zip_fd);
  2823.             if (@fseek($this->_zip_fd,  $v_header_list[$i]['offset'])) {
  2824.                 // ----- Clean
  2825.                 $this->_closeFd();
  2826.                 $v_temp_zip->_closeFd();
  2827.                 @unlink($v_zip_temp_name);
  2828.  
  2829.                 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
  2830.                                  'Invalid archive size');
  2831.                 return Archive_Zip::errorCode();
  2832.             }
  2833.  
  2834.             // ----- Read the file header
  2835.             if (($v_result = $this->_readFileHeader($v_header_list[$i])) != 1) {
  2836.                 // ----- Clean
  2837.                 $this->_closeFd();
  2838.                 $v_temp_zip->_closeFd();
  2839.                 @unlink($v_zip_temp_name);
  2840.  
  2841.                 return $v_result;
  2842.             }
  2843.  
  2844.             // ----- Write the file header
  2845.             $v_result = $v_temp_zip->_writeFileHeader($v_header_list[$i]);
  2846.             if ($v_result != 1) {
  2847.                 // ----- Clean
  2848.                 $this->_closeFd();
  2849.                 $v_temp_zip->_closeFd();
  2850.                 @unlink($v_zip_temp_name);
  2851.  
  2852.                 return $v_result;
  2853.             }
  2854.  
  2855.             // ----- Read/write the data block
  2856.             $v_result = $this->_tool_CopyBlock($this->_zip_fd,
  2857.                                                $v_temp_zip->_zip_fd,
  2858.                                        $v_header_list[$i]['compressed_size']);
  2859.             if ($v_result != 1) {
  2860.                 // ----- Clean
  2861.                 $this->_closeFd();
  2862.                 $v_temp_zip->_closeFd();
  2863.                 @unlink($v_zip_temp_name);
  2864.  
  2865.                 return $v_result;
  2866.             }
  2867.         }
  2868.  
  2869.         // ----- Store the offset of the central dir
  2870.         $v_offset = @ftell($v_temp_zip->_zip_fd);
  2871.  
  2872.         // ----- Re-Create the Central Dir files header
  2873.         for ($i=0; $i<sizeof($v_header_list); $i++) {
  2874.             // ----- Create the file header
  2875.             $v_result=$v_temp_zip->_writeCentralFileHeader($v_header_list[$i]);
  2876.             if ($v_result != 1) {
  2877.                 // ----- Clean
  2878.                 $v_temp_zip->_closeFd();
  2879.                 $this->_closeFd();
  2880.                 @unlink($v_zip_temp_name);
  2881.  
  2882.                 return $v_result;
  2883.             }
  2884.  
  2885.             // ----- Transform the header to a 'usable' info
  2886.             $v_temp_zip->_convertHeader2FileInfo($v_header_list[$i],
  2887.                                                  $p_result_list[$i]);
  2888.         }
  2889.  
  2890.  
  2891.         // ----- Zip file comment
  2892.         $v_comment = '';
  2893.  
  2894.         // ----- Calculate the size of the central header
  2895.         $v_size = @ftell($v_temp_zip->_zip_fd)-$v_offset;
  2896.  
  2897.         // ----- Create the central dir footer
  2898.         $v_result = $v_temp_zip->_writeCentralHeader(sizeof($v_header_list),
  2899.                                                      $v_size, $v_offset,
  2900.                                                      $v_comment);
  2901.         if ($v_result != 1) {
  2902.             // ----- Clean
  2903.             unset($v_header_list);
  2904.             $v_temp_zip->_closeFd();
  2905.             $this->_closeFd();
  2906.             @unlink($v_zip_temp_name);
  2907.  
  2908.             return $v_result;
  2909.         }
  2910.  
  2911.         // ----- Close
  2912.         $v_temp_zip->_closeFd();
  2913.         $this->_closeFd();
  2914.  
  2915.         // ----- Delete the zip file
  2916.         // TBC : I should test the result ...
  2917.         @unlink($this->_zipname);
  2918.  
  2919.         // ----- Rename the temporary file
  2920.         // TBC : I should test the result ...
  2921.         //@rename($v_zip_temp_name, $this->_zipname);
  2922.         $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
  2923.  
  2924.         // ----- Destroy the temporary archive
  2925.         unset($v_temp_zip);
  2926.     }
  2927.  
  2928.     // ----- Return
  2929.     return $v_result;
  2930.   }
  2931.   // ---------------------------------------------------------------------------
  2932.  
  2933.   // ---------------------------------------------------------------------------
  2934.   // Function : _dirCheck()
  2935.   // Description :
  2936.   //   Check if a directory exists, if not it creates it and all the parents directory
  2937.   //   which may be useful.
  2938.   // Parameters :
  2939.   //   $p_dir : Directory path to check.
  2940.   // Return Values :
  2941.   //    1 : OK
  2942.   //   -1 : Unable to create directory
  2943.   // ---------------------------------------------------------------------------
  2944.   /**
  2945.   * Archive_Zip::_dirCheck()
  2946.   *
  2947.   * { Description }
  2948.   *
  2949.   * @param [type] $p_is_dir
  2950.   */
  2951.   function _dirCheck($p_dir, $p_is_dir=false)
  2952.   {
  2953.     $v_result = 1;
  2954.  
  2955.     // ----- Remove the final '/'
  2956.     if (($p_is_dir) && (substr($p_dir, -1)=='/')) {
  2957.       $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
  2958.     }
  2959.  
  2960.     // ----- Check the directory availability
  2961.     if ((is_dir($p_dir)) || ($p_dir == "")) {
  2962.       return 1;
  2963.     }
  2964.  
  2965.     // ----- Extract parent directory
  2966.     $p_parent_dir = dirname($p_dir);
  2967.  
  2968.     // ----- Just a check
  2969.     if ($p_parent_dir != $p_dir) {
  2970.       // ----- Look for parent directory
  2971.       if ($p_parent_dir != "") {
  2972.         if (($v_result = $this->_dirCheck($p_parent_dir)) != 1) {
  2973.           return $v_result;
  2974.         }
  2975.       }
  2976.     }
  2977.  
  2978.     // ----- Create the directory
  2979.     if (!@mkdir($p_dir, 0777)) {
  2980.       $this->_errorLog(ARCHIVE_ZIP_ERR_DIR_CREATE_FAIL,
  2981.                        "Unable to create directory '$p_dir'");
  2982.       return Archive_Zip::errorCode();
  2983.     }
  2984.  
  2985.     // ----- Return
  2986.     return $v_result;
  2987.   }
  2988.   // ---------------------------------------------------------------------------
  2989.  
  2990.   // ---------------------------------------------------------------------------
  2991.   // Function : _merge()
  2992.   // Description :
  2993.   //   If $p_archive_to_add does not exist, the function exit with a success result.
  2994.   // Parameters :
  2995.   // Return Values :
  2996.   // ---------------------------------------------------------------------------
  2997.   /**
  2998.   * Archive_Zip::_merge()
  2999.   *
  3000.   * { Description }
  3001.   *
  3002.   */
  3003.   function _merge(&$p_archive_to_add)
  3004.   {
  3005.     $v_result=1;
  3006.  
  3007.     // ----- Look if the archive_to_add exists
  3008.     if (!is_file($p_archive_to_add->_zipname)) {
  3009.       // ----- Nothing to merge, so merge is a success
  3010.       return 1;
  3011.     }
  3012.  
  3013.     // ----- Look if the archive exists
  3014.     if (!is_file($this->_zipname)) {
  3015.       // ----- Do a duplicate
  3016.       $v_result = $this->_duplicate($p_archive_to_add->_zipname);
  3017.  
  3018.       return $v_result;
  3019.     }
  3020.  
  3021.     // ----- Open the zip file
  3022.     if (($v_result=$this->_openFd('rb')) != 1) {
  3023.       return $v_result;
  3024.     }
  3025.  
  3026.     // ----- Read the central directory informations
  3027.     $v_central_dir = array();
  3028.     if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
  3029.       $this->_closeFd();
  3030.       return $v_result;
  3031.     }
  3032.  
  3033.     // ----- Go to beginning of File
  3034.     @rewind($this->_zip_fd);
  3035.  
  3036.     // ----- Open the archive_to_add file
  3037.     if (($v_result=$p_archive_to_add->_openFd('rb')) != 1) {
  3038.       $this->_closeFd();
  3039.       return $v_result;
  3040.     }
  3041.  
  3042.     // ----- Read the central directory informations
  3043.     $v_central_dir_to_add = array();
  3044.     $v_result = $p_archive_to_add->_readEndCentralDir($v_central_dir_to_add);
  3045.     if ($v_result != 1) {
  3046.       $this->_closeFd();
  3047.       $p_archive_to_add->_closeFd();
  3048.       return $v_result;
  3049.     }
  3050.  
  3051.     // ----- Go to beginning of File
  3052.     @rewind($p_archive_to_add->_zip_fd);
  3053.  
  3054.     // ----- Creates a temporay file
  3055.     $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR.uniqid('archive_zip-').'.tmp';
  3056.  
  3057.     // ----- Open the temporary file in write mode
  3058.     if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  3059.       $this->_closeFd();
  3060.       $p_archive_to_add->_closeFd();
  3061.       $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  3062.                        'Unable to open temporary file \''
  3063.                        .$v_zip_temp_name.'\' in binary write mode');
  3064.       return Archive_Zip::errorCode();
  3065.     }
  3066.  
  3067.     // ----- Copy the files from the archive to the temporary file
  3068.     // TBC : Here I should better append the file and go back to erase the
  3069.     // central dir
  3070.     $v_size = $v_central_dir['offset'];
  3071.     while ($v_size != 0) {
  3072.       $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  3073.                       ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  3074.       $v_buffer = fread($this->_zip_fd, $v_read_size);
  3075.       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  3076.       $v_size -= $v_read_size;
  3077.     }
  3078.  
  3079.     // ----- Copy the files from the archive_to_add into the temporary file
  3080.     $v_size = $v_central_dir_to_add['offset'];
  3081.     while ($v_size != 0) {
  3082.       $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  3083.                       ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  3084.       $v_buffer = fread($p_archive_to_add->_zip_fd, $v_read_size);
  3085.       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  3086.       $v_size -= $v_read_size;
  3087.     }
  3088.  
  3089.     // ----- Store the offset of the central dir
  3090.     $v_offset = @ftell($v_zip_temp_fd);
  3091.  
  3092.     // ----- Copy the block of file headers from the old archive
  3093.     $v_size = $v_central_dir['size'];
  3094.     while ($v_size != 0) {
  3095.       $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  3096.                       ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  3097.       $v_buffer = @fread($this->_zip_fd, $v_read_size);
  3098.       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  3099.       $v_size -= $v_read_size;
  3100.     }
  3101.  
  3102.     // ----- Copy the block of file headers from the archive_to_add
  3103.     $v_size = $v_central_dir_to_add['size'];
  3104.     while ($v_size != 0) {
  3105.       $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  3106.                       ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  3107.       $v_buffer = @fread($p_archive_to_add->_zip_fd, $v_read_size);
  3108.       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  3109.       $v_size -= $v_read_size;
  3110.     }
  3111.  
  3112.     // ----- Zip file comment
  3113.     // TBC : I should merge the two comments
  3114.     $v_comment = '';
  3115.  
  3116.     // ----- Calculate the size of the (new) central header
  3117.     $v_size = @ftell($v_zip_temp_fd)-$v_offset;
  3118.  
  3119.     // ----- Swap the file descriptor
  3120.     // Here is a trick : I swap the temporary fd with the zip fd, in order to use
  3121.     // the following methods on the temporary fil and not the real archive fd
  3122.     $v_swap = $this->_zip_fd;
  3123.     $this->_zip_fd = $v_zip_temp_fd;
  3124.     $v_zip_temp_fd = $v_swap;
  3125.  
  3126.     // ----- Create the central dir footer
  3127.     if (($v_result = $this->_writeCentralHeader($v_central_dir['entries']
  3128.                                               +$v_central_dir_to_add['entries'],
  3129.                                                 $v_size, $v_offset,
  3130.                                                 $v_comment)) != 1) {
  3131.       $this->_closeFd();
  3132.       $p_archive_to_add->_closeFd();
  3133.       @fclose($v_zip_temp_fd);
  3134.       $this->_zip_fd = null;
  3135.  
  3136.       // ----- Reset the file list
  3137.       unset($v_header_list);
  3138.  
  3139.       // ----- Return
  3140.       return $v_result;
  3141.     }
  3142.  
  3143.     // ----- Swap back the file descriptor
  3144.     $v_swap = $this->_zip_fd;
  3145.     $this->_zip_fd = $v_zip_temp_fd;
  3146.     $v_zip_temp_fd = $v_swap;
  3147.  
  3148.     // ----- Close
  3149.     $this->_closeFd();
  3150.     $p_archive_to_add->_closeFd();
  3151.  
  3152.     // ----- Close the temporary file
  3153.     @fclose($v_zip_temp_fd);
  3154.  
  3155.     // ----- Delete the zip file
  3156.     // TBC : I should test the result ...
  3157.     @unlink($this->_zipname);
  3158.  
  3159.     // ----- Rename the temporary file
  3160.     // TBC : I should test the result ...
  3161.     //@rename($v_zip_temp_name, $this->_zipname);
  3162.     $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
  3163.  
  3164.     // ----- Return
  3165.     return $v_result;
  3166.   }
  3167.   // ---------------------------------------------------------------------------
  3168.  
  3169.   // ---------------------------------------------------------------------------
  3170.   // Function : _duplicate()
  3171.   // Description :
  3172.   // Parameters :
  3173.   // Return Values :
  3174.   // ---------------------------------------------------------------------------
  3175.   /**
  3176.   * Archive_Zip::_duplicate()
  3177.   *
  3178.   * { Description }
  3179.   *
  3180.   */
  3181.   function _duplicate($p_archive_filename)
  3182.   {
  3183.     $v_result=1;
  3184.  
  3185.     // ----- Look if the $p_archive_filename exists
  3186.     if (!is_file($p_archive_filename)) {
  3187.  
  3188.       // ----- Nothing to duplicate, so duplicate is a success.
  3189.       $v_result = 1;
  3190.  
  3191.       // ----- Return
  3192.       return $v_result;
  3193.     }
  3194.  
  3195.     // ----- Open the zip file
  3196.     if (($v_result=$this->_openFd('wb')) != 1) {
  3197.       // ----- Return
  3198.       return $v_result;
  3199.     }
  3200.  
  3201.     // ----- Open the temporary file in write mode
  3202.     if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) {
  3203.       $this->_closeFd();
  3204.       $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  3205.                        'Unable to open archive file \''
  3206.                        .$p_archive_filename.'\' in binary write mode');
  3207.       return Archive_Zip::errorCode();
  3208.     }
  3209.  
  3210.     // ----- Copy the files from the archive to the temporary file
  3211.     // TBC : Here I should better append the file and go back to erase the
  3212.     // central dir
  3213.     $v_size = filesize($p_archive_filename);
  3214.     while ($v_size != 0) {
  3215.       $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  3216.                       ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  3217.       $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  3218.       @fwrite($this->_zip_fd, $v_buffer, $v_read_size);
  3219.       $v_size -= $v_read_size;
  3220.     }
  3221.  
  3222.     // ----- Close
  3223.     $this->_closeFd();
  3224.  
  3225.     // ----- Close the temporary file
  3226.     @fclose($v_zip_temp_fd);
  3227.  
  3228.     return $v_result;
  3229.   }
  3230.   // ---------------------------------------------------------------------------
  3231.  
  3232.   /**
  3233.   * Archive_Zip::_check_parameters()
  3234.   *
  3235.   * { Description }
  3236.   *
  3237.   * @param integer $p_error_code
  3238.   * @param string $p_error_string
  3239.   */
  3240.   function _check_parameters(&$p_params, $p_default)
  3241.   {
  3242.     
  3243.     // ----- Check that param is an array
  3244.     if (!is_array($p_params)) {
  3245.         $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  3246.                          'Unsupported parameter, waiting for an array');
  3247.         return Archive_Zip::errorCode();
  3248.     }
  3249.     
  3250.     // ----- Check that all the params are valid
  3251.     for (reset($p_params); list($v_key, $v_value) = each($p_params); ) {
  3252.         if (!isset($p_default[$v_key])) {
  3253.             $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  3254.                              'Unsupported parameter with key \''.$v_key.'\'');
  3255.  
  3256.             return Archive_Zip::errorCode();
  3257.         }
  3258.     }
  3259.  
  3260.     // ----- Set the default values
  3261.     for (reset($p_default); list($v_key, $v_value) = each($p_default); ) {
  3262.         if (!isset($p_params[$v_key])) {
  3263.             $p_params[$v_key] = $p_default[$v_key];
  3264.         }
  3265.     }
  3266.     
  3267.     // ----- Check specific parameters
  3268.     $v_callback_list = array ('callback_pre_add','callback_post_add',
  3269.                               'callback_pre_extract','callback_post_extract');
  3270.     for ($i=0; $i<sizeof($v_callback_list); $i++) {
  3271.         $v_key=$v_callback_list[$i];
  3272.         if (   (isset($p_params[$v_key])) && ($p_params[$v_key] != '')) {
  3273.             if (!function_exists($p_params[$v_key])) {
  3274.                 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAM_VALUE,
  3275.                                  "Callback '".$p_params[$v_key]
  3276.                                  ."()' is not an existing function for "
  3277.                                  ."parameter '".$v_key."'");
  3278.                 return Archive_Zip::errorCode();
  3279.             }
  3280.         }
  3281.     }
  3282.  
  3283.     return(1);
  3284.   }
  3285.   // ---------------------------------------------------------------------------
  3286.  
  3287.   // ---------------------------------------------------------------------------
  3288.   // Function : _errorLog()
  3289.   // Description :
  3290.   // Parameters :
  3291.   // ---------------------------------------------------------------------------
  3292.   /**
  3293.   * Archive_Zip::_errorLog()
  3294.   *
  3295.   * { Description }
  3296.   *
  3297.   * @param integer $p_error_code
  3298.   * @param string $p_error_string
  3299.   */
  3300.   function _errorLog($p_error_code=0, $p_error_string='')
  3301.   {
  3302.       $this->_error_code = $p_error_code;
  3303.       $this->_error_string = $p_error_string;
  3304.   }
  3305.   // ---------------------------------------------------------------------------
  3306.  
  3307.   // ---------------------------------------------------------------------------
  3308.   // Function : _errorReset()
  3309.   // Description :
  3310.   // Parameters :
  3311.   // ---------------------------------------------------------------------------
  3312.   /**
  3313.   * Archive_Zip::_errorReset()
  3314.   *
  3315.   * { Description }
  3316.   *
  3317.   */
  3318.   function _errorReset()
  3319.   {
  3320.       $this->_error_code = 1;
  3321.       $this->_error_string = '';
  3322.   }
  3323.   // ---------------------------------------------------------------------------
  3324.  
  3325.   // ---------------------------------------------------------------------------
  3326.   // Function : $this->_tool_PathReduction()
  3327.   // Description :
  3328.   // Parameters :
  3329.   // Return Values :
  3330.   // ---------------------------------------------------------------------------
  3331.   /**
  3332.   * _tool_PathReduction()
  3333.   *
  3334.   * { Description }
  3335.   *
  3336.   */
  3337.   function _tool_PathReduction($p_dir)
  3338.   {
  3339.     $v_result = "";
  3340.  
  3341.     // ----- Look for not empty path
  3342.     if ($p_dir != "")
  3343.     {
  3344.       // ----- Explode path by directory names
  3345.       $v_list = explode("/", $p_dir);
  3346.  
  3347.       // ----- Study directories from last to first
  3348.       for ($i=sizeof($v_list)-1; $i>=0; $i--)
  3349.       {
  3350.         // ----- Look for current path
  3351.         if ($v_list[$i] == ".")
  3352.         {
  3353.           // ----- Ignore this directory
  3354.           // Should be the first $i=0, but no check is done
  3355.         }
  3356.         else if ($v_list[$i] == "..")
  3357.         {
  3358.           // ----- Ignore it and ignore the $i-1
  3359.           $i--;
  3360.         }
  3361.         else if (($v_list[$i] == "") && ($i!=(sizeof($v_list)-1)) && ($i!=0))
  3362.         {
  3363.           // ----- Ignore only the double '//' in path,
  3364.           // but not the first and last '/'
  3365.         }
  3366.         else
  3367.         {
  3368.           $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  3369.         }
  3370.       }
  3371.     }
  3372.  
  3373.     // ----- Return
  3374.     return $v_result;
  3375.   }
  3376.   // ---------------------------------------------------------------------------
  3377.  
  3378.   // ---------------------------------------------------------------------------
  3379.   // Function : $this->_tool_PathInclusion()
  3380.   // Description :
  3381.   //   This function indicates if the path $p_path is under the $p_dir tree. Or,
  3382.   //   said in an other way, if the file or sub-dir $p_path is inside the dir
  3383.   //   $p_dir.
  3384.   //   The function indicates also if the path is exactly the same as the dir.
  3385.   //   This function supports path with duplicated '/' like '//', but does not
  3386.   //   support '.' or '..' statements.
  3387.   // Parameters :
  3388.   // Return Values :
  3389.   //   0 if $p_path is not inside directory $p_dir
  3390.   //   1 if $p_path is inside directory $p_dir
  3391.   //   2 if $p_path is exactly the same as $p_dir
  3392.   // ---------------------------------------------------------------------------
  3393.   /**
  3394.   * _tool_PathInclusion()
  3395.   *
  3396.   * { Description }
  3397.   *
  3398.   */
  3399.   function _tool_PathInclusion($p_dir, $p_path)
  3400.   {
  3401.     $v_result = 1;
  3402.  
  3403.     // ----- Explode dir and path by directory separator
  3404.     $v_list_dir = explode("/", $p_dir);
  3405.     $v_list_dir_size = sizeof($v_list_dir);
  3406.     $v_list_path = explode("/", $p_path);
  3407.     $v_list_path_size = sizeof($v_list_path);
  3408.  
  3409.     // ----- Study directories paths
  3410.     $i = 0;
  3411.     $j = 0;
  3412.     while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  3413.  
  3414.       // ----- Look for empty dir (path reduction)
  3415.       if ($v_list_dir[$i] == '') {
  3416.         $i++;
  3417.         continue;
  3418.       }
  3419.       if ($v_list_path[$j] == '') {
  3420.         $j++;
  3421.         continue;
  3422.       }
  3423.  
  3424.       // ----- Compare the items
  3425.       if (   ($v_list_dir[$i] != $v_list_path[$j])
  3426.           && ($v_list_dir[$i] != '')
  3427.           && ( $v_list_path[$j] != ''))  {
  3428.         $v_result = 0;
  3429.       }
  3430.  
  3431.       // ----- Next items
  3432.       $i++;
  3433.       $j++;
  3434.     }
  3435.  
  3436.     // ----- Look if everything seems to be the same
  3437.     if ($v_result) {
  3438.       // ----- Skip all the empty items
  3439.       while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
  3440.       while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
  3441.  
  3442.       if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  3443.         // ----- There are exactly the same
  3444.         $v_result = 2;
  3445.       }
  3446.       else if ($i < $v_list_dir_size) {
  3447.         // ----- The path is shorter than the dir
  3448.         $v_result = 0;
  3449.       }
  3450.     }
  3451.  
  3452.     // ----- Return
  3453.     return $v_result;
  3454.   }
  3455.   // ---------------------------------------------------------------------------
  3456.  
  3457.   // ---------------------------------------------------------------------------
  3458.   // Function : $this->_tool_CopyBlock()
  3459.   // Description :
  3460.   // Parameters :
  3461.   //   $p_mode : read/write compression mode
  3462.   //             0 : src & dest normal
  3463.   //             1 : src gzip, dest normal
  3464.   //             2 : src normal, dest gzip
  3465.   //             3 : src & dest gzip
  3466.   // Return Values :
  3467.   // ---------------------------------------------------------------------------
  3468.   /**
  3469.   * _tool_CopyBlock()
  3470.   *
  3471.   * { Description }
  3472.   *
  3473.   * @param integer $p_mode
  3474.   */
  3475.   function _tool_CopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
  3476.   {
  3477.     $v_result = 1;
  3478.  
  3479.     if ($p_mode==0)
  3480.     {
  3481.       while ($p_size != 0)
  3482.       {
  3483.         $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  3484.                         ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  3485.         $v_buffer = @fread($p_src, $v_read_size);
  3486.         @fwrite($p_dest, $v_buffer, $v_read_size);
  3487.         $p_size -= $v_read_size;
  3488.       }
  3489.     }
  3490.     else if ($p_mode==1)
  3491.     {
  3492.       while ($p_size != 0)
  3493.       {
  3494.         $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  3495.                         ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  3496.         $v_buffer = @gzread($p_src, $v_read_size);
  3497.         @fwrite($p_dest, $v_buffer, $v_read_size);
  3498.         $p_size -= $v_read_size;
  3499.       }
  3500.     }
  3501.     else if ($p_mode==2)
  3502.     {
  3503.       while ($p_size != 0)
  3504.       {
  3505.         $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  3506.                         ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  3507.         $v_buffer = @fread($p_src, $v_read_size);
  3508.         @gzwrite($p_dest, $v_buffer, $v_read_size);
  3509.         $p_size -= $v_read_size;
  3510.       }
  3511.     }
  3512.     else if ($p_mode==3)
  3513.     {
  3514.       while ($p_size != 0)
  3515.       {
  3516.         $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  3517.                         ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  3518.         $v_buffer = @gzread($p_src, $v_read_size);
  3519.         @gzwrite($p_dest, $v_buffer, $v_read_size);
  3520.         $p_size -= $v_read_size;
  3521.       }
  3522.     }
  3523.  
  3524.     // ----- Return
  3525.     return $v_result;
  3526.   }
  3527.   // ---------------------------------------------------------------------------
  3528.  
  3529.   // ---------------------------------------------------------------------------
  3530.   // Function : $this->_tool_Rename()
  3531.   // Description :
  3532.   //   This function tries to do a simple rename() function. If it fails, it
  3533.   //   tries to copy the $p_src file in a new $p_dest file and then unlink the
  3534.   //   first one.
  3535.   // Parameters :
  3536.   //   $p_src : Old filename
  3537.   //   $p_dest : New filename
  3538.   // Return Values :
  3539.   //   1 on success, 0 on failure.
  3540.   // ---------------------------------------------------------------------------
  3541.   /**
  3542.   * _tool_Rename()
  3543.   *
  3544.   * { Description }
  3545.   *
  3546.   */
  3547.   function _tool_Rename($p_src, $p_dest)
  3548.   {
  3549.     $v_result = 1;
  3550.  
  3551.     // ----- Try to rename the files
  3552.     if (!@rename($p_src, $p_dest)) {
  3553.  
  3554.       // ----- Try to copy & unlink the src
  3555.       if (!@copy($p_src, $p_dest)) {
  3556.         $v_result = 0;
  3557.       }
  3558.       else if (!@unlink($p_src)) {
  3559.         $v_result = 0;
  3560.       }
  3561.     }
  3562.  
  3563.     // ----- Return
  3564.     return $v_result;
  3565.   }
  3566.   // ---------------------------------------------------------------------------
  3567.  
  3568.   // ---------------------------------------------------------------------------
  3569.   // Function : $this->_tool_TranslateWinPath()
  3570.   // Description :
  3571.   //   Translate windows path by replacing '\' by '/' and optionally removing
  3572.   //   drive letter.
  3573.   // Parameters :
  3574.   //   $p_path : path to translate.
  3575.   //   $p_remove_disk_letter : true | false
  3576.   // Return Values :
  3577.   //   The path translated.
  3578.   // ---------------------------------------------------------------------------
  3579.   /**
  3580.   * _tool_TranslateWinPath()
  3581.   *
  3582.   * { Description }
  3583.   *
  3584.   * @param [type] $p_remove_disk_letter
  3585.   */
  3586.   function _tool_TranslateWinPath($p_path, $p_remove_disk_letter=true)
  3587.   {
  3588.     if (stristr(php_uname(), 'windows')) {
  3589.       // ----- Look for potential disk letter
  3590.       if (   ($p_remove_disk_letter)
  3591.           && (($v_position = strpos($p_path, ':')) != false)) {
  3592.           $p_path = substr($p_path, $v_position+1);
  3593.       }
  3594.       // ----- Change potential windows directory separator
  3595.       if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
  3596.           $p_path = strtr($p_path, '\\', '/');
  3597.       }
  3598.     }
  3599.     return $p_path;
  3600.   }
  3601.   // ---------------------------------------------------------------------------
  3602.  
  3603.   }
  3604.   // End of class
  3605.  
  3606. ?>
  3607.