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