web/wp-admin/includes/class-pclzip.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
     1 <?php
     1 <?php
     2 // --------------------------------------------------------------------------------
     2 // --------------------------------------------------------------------------------
     3 // PhpConcept Library - Zip Module 2.8
     3 // PhpConcept Library - Zip Module 2.8.2
     4 // --------------------------------------------------------------------------------
     4 // --------------------------------------------------------------------------------
     5 // License GNU/LGPL - Vincent Blavet - March 2006
     5 // License GNU/LGPL - Vincent Blavet - August 2009
     6 // http://www.phpconcept.net
     6 // http://www.phpconcept.net
     7 // --------------------------------------------------------------------------------
     7 // --------------------------------------------------------------------------------
     8 //
     8 //
     9 // Presentation :
     9 // Presentation :
    10 //   PclZip is a PHP library that manage ZIP archives.
    10 //   PclZip is a PHP library that manage ZIP archives.
    20 //   It should not have unexpected results. However if any damage is caused by
    20 //   It should not have unexpected results. However if any damage is caused by
    21 //   this software the author can not be responsible.
    21 //   this software the author can not be responsible.
    22 //   The use of this software is at the risk of the user.
    22 //   The use of this software is at the risk of the user.
    23 //
    23 //
    24 // --------------------------------------------------------------------------------
    24 // --------------------------------------------------------------------------------
    25 // $Id: pclzip.lib.php,v 1.55 2009/04/22 07:38:36 vblavet Exp $
    25 // $Id: pclzip.lib.php,v 1.60 2009/09/30 21:01:04 vblavet Exp $
    26 // --------------------------------------------------------------------------------
    26 // --------------------------------------------------------------------------------
    27 
    27 
    28   // ----- Constants
    28   // ----- Constants
    29   if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
    29   if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
    30     define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
    30     define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
    31   }
    31   }
    32   
    32 
    33   // ----- File list separator
    33   // ----- File list separator
    34   // In version 1.x of PclZip, the separator for file list is a space
    34   // In version 1.x of PclZip, the separator for file list is a space
    35   // (which is not a very smart choice, specifically for windows paths !).
    35   // (which is not a very smart choice, specifically for windows paths !).
    36   // A better separator should be a comma (,). This constant gives you the
    36   // A better separator should be a comma (,). This constant gives you the
    37   // abilty to change that.
    37   // abilty to change that.
    66     define( 'PCLZIP_TEMPORARY_DIR', '' );
    66     define( 'PCLZIP_TEMPORARY_DIR', '' );
    67   }
    67   }
    68 
    68 
    69   // ----- Optional threshold ratio for use of temporary files
    69   // ----- Optional threshold ratio for use of temporary files
    70   //       Pclzip sense the size of the file to add/extract and decide to
    70   //       Pclzip sense the size of the file to add/extract and decide to
    71   //       use or not temporary file. The algorythm is looking for 
    71   //       use or not temporary file. The algorythm is looking for
    72   //       memory_limit of PHP and apply a ratio.
    72   //       memory_limit of PHP and apply a ratio.
    73   //       threshold = memory_limit * ratio.
    73   //       threshold = memory_limit * ratio.
    74   //       Recommended values are under 0.5. Default 0.47.
    74   //       Recommended values are under 0.5. Default 0.47.
    75   //       Samples :
    75   //       Samples :
    76   // define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.5 );
    76   // define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.5 );
    81 // --------------------------------------------------------------------------------
    81 // --------------------------------------------------------------------------------
    82 // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
    82 // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
    83 // --------------------------------------------------------------------------------
    83 // --------------------------------------------------------------------------------
    84 
    84 
    85   // ----- Global variables
    85   // ----- Global variables
    86   $g_pclzip_version = "2.8";
    86   $g_pclzip_version = "2.8.2";
    87 
    87 
    88   // ----- Error codes
    88   // ----- Error codes
    89   //   -1 : Unable to open file in binary write mode
    89   //   -1 : Unable to open file in binary write mode
    90   //   -2 : Unable to open file in binary read mode
    90   //   -2 : Unable to open file in binary read mode
    91   //   -3 : Invalid parameters
    91   //   -3 : Invalid parameters
   150   define( 'PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020 ); // alias
   150   define( 'PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020 ); // alias
   151   define( 'PCLZIP_OPT_TEMP_FILE_ON', 77021 );
   151   define( 'PCLZIP_OPT_TEMP_FILE_ON', 77021 );
   152   define( 'PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021 ); // alias
   152   define( 'PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021 ); // alias
   153   define( 'PCLZIP_OPT_TEMP_FILE_OFF', 77022 );
   153   define( 'PCLZIP_OPT_TEMP_FILE_OFF', 77022 );
   154   define( 'PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022 ); // alias
   154   define( 'PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022 ); // alias
   155   
   155 
   156   // ----- File description attributes
   156   // ----- File description attributes
   157   define( 'PCLZIP_ATT_FILE_NAME', 79001 );
   157   define( 'PCLZIP_ATT_FILE_NAME', 79001 );
   158   define( 'PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002 );
   158   define( 'PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002 );
   159   define( 'PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003 );
   159   define( 'PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003 );
   160   define( 'PCLZIP_ATT_FILE_MTIME', 79004 );
   160   define( 'PCLZIP_ATT_FILE_MTIME', 79004 );
   196     var $zip_fd = 0;
   196     var $zip_fd = 0;
   197 
   197 
   198     // ----- Internal error handling
   198     // ----- Internal error handling
   199     var $error_code = 1;
   199     var $error_code = 1;
   200     var $error_string = '';
   200     var $error_string = '';
   201     
   201 
   202     // ----- Current status of the magic_quotes_runtime
   202     // ----- Current status of the magic_quotes_runtime
   203     // This value store the php configuration for magic_quotes
   203     // This value store the php configuration for magic_quotes
   204     // The class can then disable the magic_quotes and reset it after
   204     // The class can then disable the magic_quotes and reset it after
   205     var $magic_quotes_status;
   205     var $magic_quotes_status;
   206 
   206 
   212   //   Note that no real action is taken, if the archive does not exist it is not
   212   //   Note that no real action is taken, if the archive does not exist it is not
   213   //   created. Use create() for that.
   213   //   created. Use create() for that.
   214   // --------------------------------------------------------------------------------
   214   // --------------------------------------------------------------------------------
   215   function PclZip($p_zipname)
   215   function PclZip($p_zipname)
   216   {
   216   {
   217     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::PclZip', "zipname=$p_zipname");
       
   218 
   217 
   219     // ----- Tests the zlib
   218     // ----- Tests the zlib
   220     if (!function_exists('gzopen'))
   219     if (!function_exists('gzopen'))
   221     {
   220     {
   222       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 1, "zlib extension seems to be missing");
       
   223       die('Abort '.basename(__FILE__).' : Missing zlib extensions');
   221       die('Abort '.basename(__FILE__).' : Missing zlib extensions');
   224     }
   222     }
   225 
   223 
   226     // ----- Set the attributes
   224     // ----- Set the attributes
   227     $this->zipname = $p_zipname;
   225     $this->zipname = $p_zipname;
   228     $this->zip_fd = 0;
   226     $this->zip_fd = 0;
   229     $this->magic_quotes_status = -1;
   227     $this->magic_quotes_status = -1;
   230 
   228 
   231     // ----- Return
   229     // ----- Return
   232     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 1);
       
   233     return;
   230     return;
   234   }
   231   }
   235   // --------------------------------------------------------------------------------
   232   // --------------------------------------------------------------------------------
   236 
   233 
   237   // --------------------------------------------------------------------------------
   234   // --------------------------------------------------------------------------------
   271   //   The list of the added files, with a status of the add action.
   268   //   The list of the added files, with a status of the add action.
   272   //   (see PclZip::listContent() for list entry format)
   269   //   (see PclZip::listContent() for list entry format)
   273   // --------------------------------------------------------------------------------
   270   // --------------------------------------------------------------------------------
   274   function create($p_filelist)
   271   function create($p_filelist)
   275   {
   272   {
   276     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::create', "filelist='$p_filelist', ...");
       
   277     $v_result=1;
   273     $v_result=1;
   278 
   274 
   279     // ----- Reset the error handler
   275     // ----- Reset the error handler
   280     $this->privErrorReset();
   276     $this->privErrorReset();
   281 
   277 
   283     $v_options = array();
   279     $v_options = array();
   284     $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
   280     $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
   285 
   281 
   286     // ----- Look for variable options arguments
   282     // ----- Look for variable options arguments
   287     $v_size = func_num_args();
   283     $v_size = func_num_args();
   288     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
       
   289 
   284 
   290     // ----- Look for arguments
   285     // ----- Look for arguments
   291     if ($v_size > 1) {
   286     if ($v_size > 1) {
   292       // ----- Get the arguments
   287       // ----- Get the arguments
   293       $v_arg_list = func_get_args();
   288       $v_arg_list = func_get_args();
   296       array_shift($v_arg_list);
   291       array_shift($v_arg_list);
   297       $v_size--;
   292       $v_size--;
   298 
   293 
   299       // ----- Look for first arg
   294       // ----- Look for first arg
   300       if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
   295       if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
   301         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
       
   302 
   296 
   303         // ----- Parse the options
   297         // ----- Parse the options
   304         $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
   298         $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
   305                                             array (PCLZIP_OPT_REMOVE_PATH => 'optional',
   299                                             array (PCLZIP_OPT_REMOVE_PATH => 'optional',
   306                                                    PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
   300                                                    PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
   313                                                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
   307                                                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
   314                                                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
   308                                                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
   315                                                    //, PCLZIP_OPT_CRYPT => 'optional'
   309                                                    //, PCLZIP_OPT_CRYPT => 'optional'
   316                                              ));
   310                                              ));
   317         if ($v_result != 1) {
   311         if ($v_result != 1) {
   318           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   319           return 0;
   312           return 0;
   320         }
   313         }
   321       }
   314       }
   322 
   315 
   323       // ----- Look for 2 args
   316       // ----- Look for 2 args
   324       // Here we need to support the first historic synopsis of the
   317       // Here we need to support the first historic synopsis of the
   325       // method.
   318       // method.
   326       else {
   319       else {
   327         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
       
   328 
   320 
   329         // ----- Get the first argument
   321         // ----- Get the first argument
   330         $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
   322         $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
   331 
   323 
   332         // ----- Look for the optional second argument
   324         // ----- Look for the optional second argument
   334           $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
   326           $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
   335         }
   327         }
   336         else if ($v_size > 2) {
   328         else if ($v_size > 2) {
   337           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
   329           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
   338 		                       "Invalid number / type of arguments");
   330 		                       "Invalid number / type of arguments");
   339           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
   340           return 0;
   331           return 0;
   341         }
   332         }
   342       }
   333       }
   343     }
   334     }
   344     
   335 
   345     // ----- Look for default option values
   336     // ----- Look for default option values
   346     $this->privOptionDefaultThreshold($v_options);
   337     $this->privOptionDefaultThreshold($v_options);
   347 
   338 
   348     // ----- Init
   339     // ----- Init
   349     $v_string_list = array();
   340     $v_string_list = array();
   350     $v_att_list = array();
   341     $v_att_list = array();
   351     $v_filedescr_list = array();
   342     $v_filedescr_list = array();
   352     $p_result_list = array();
   343     $p_result_list = array();
   353     
   344 
   354     // ----- Look if the $p_filelist is really an array
   345     // ----- Look if the $p_filelist is really an array
   355     if (is_array($p_filelist)) {
   346     if (is_array($p_filelist)) {
   356     
   347 
   357       // ----- Look if the first element is also an array
   348       // ----- Look if the first element is also an array
   358       //       This will mean that this is a file description entry
   349       //       This will mean that this is a file description entry
   359       if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
   350       if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
   360         $v_att_list = $p_filelist;
   351         $v_att_list = $p_filelist;
   361       }
   352       }
   362       
   353 
   363       // ----- The list is a list of string names
   354       // ----- The list is a list of string names
   364       else {
   355       else {
   365         $v_string_list = $p_filelist;
   356         $v_string_list = $p_filelist;
   366       }
   357       }
   367     }
   358     }
   373     }
   364     }
   374 
   365 
   375     // ----- Invalid variable type for $p_filelist
   366     // ----- Invalid variable type for $p_filelist
   376     else {
   367     else {
   377       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
   368       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
   378       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   379       return 0;
   369       return 0;
   380     }
   370     }
   381     
   371 
   382     // ----- Reformat the string list
   372     // ----- Reformat the string list
   383     if (sizeof($v_string_list) != 0) {
   373     if (sizeof($v_string_list) != 0) {
   384       foreach ($v_string_list as $v_string) {
   374       foreach ($v_string_list as $v_string) {
   385         if ($v_string != '') {
   375         if ($v_string != '') {
   386           $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
   376           $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
   387         }
   377         }
   388         else {
   378         else {
   389           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Ignore an empty filename");
   379         }
   390         }
   380       }
   391       }
   381     }
   392     }
   382 
   393     
       
   394     // ----- For each file in the list check the attributes
   383     // ----- For each file in the list check the attributes
   395     $v_supported_attributes
   384     $v_supported_attributes
   396     = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
   385     = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
   397              ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
   386              ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
   398              ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
   387              ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
   404       $v_result = $this->privFileDescrParseAtt($v_entry,
   393       $v_result = $this->privFileDescrParseAtt($v_entry,
   405                                                $v_filedescr_list[],
   394                                                $v_filedescr_list[],
   406                                                $v_options,
   395                                                $v_options,
   407                                                $v_supported_attributes);
   396                                                $v_supported_attributes);
   408       if ($v_result != 1) {
   397       if ($v_result != 1) {
   409         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   410         return 0;
   398         return 0;
   411       }
   399       }
   412     }
   400     }
   413 
   401 
   414     // ----- Expand the filelist (expand directories)
   402     // ----- Expand the filelist (expand directories)
   415     $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
   403     $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
   416     if ($v_result != 1) {
   404     if ($v_result != 1) {
   417       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   418       return 0;
   405       return 0;
   419     }
   406     }
   420 
   407 
   421     // ----- Call the create fct
   408     // ----- Call the create fct
   422     $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
   409     $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
   423     if ($v_result != 1) {
   410     if ($v_result != 1) {
   424       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   425       return 0;
   411       return 0;
   426     }
   412     }
   427 
   413 
   428     // ----- Return
   414     // ----- Return
   429     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
       
   430     return $p_result_list;
   415     return $p_result_list;
   431   }
   416   }
   432   // --------------------------------------------------------------------------------
   417   // --------------------------------------------------------------------------------
   433 
   418 
   434   // --------------------------------------------------------------------------------
   419   // --------------------------------------------------------------------------------
   466   //   The list of the added files, with a status of the add action.
   451   //   The list of the added files, with a status of the add action.
   467   //   (see PclZip::listContent() for list entry format)
   452   //   (see PclZip::listContent() for list entry format)
   468   // --------------------------------------------------------------------------------
   453   // --------------------------------------------------------------------------------
   469   function add($p_filelist)
   454   function add($p_filelist)
   470   {
   455   {
   471     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::add', "filelist='$p_filelist', ...");
       
   472     $v_result=1;
   456     $v_result=1;
   473 
   457 
   474     // ----- Reset the error handler
   458     // ----- Reset the error handler
   475     $this->privErrorReset();
   459     $this->privErrorReset();
   476 
   460 
   478     $v_options = array();
   462     $v_options = array();
   479     $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
   463     $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
   480 
   464 
   481     // ----- Look for variable options arguments
   465     // ----- Look for variable options arguments
   482     $v_size = func_num_args();
   466     $v_size = func_num_args();
   483     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
       
   484 
   467 
   485     // ----- Look for arguments
   468     // ----- Look for arguments
   486     if ($v_size > 1) {
   469     if ($v_size > 1) {
   487       // ----- Get the arguments
   470       // ----- Get the arguments
   488       $v_arg_list = func_get_args();
   471       $v_arg_list = func_get_args();
   491       array_shift($v_arg_list);
   474       array_shift($v_arg_list);
   492       $v_size--;
   475       $v_size--;
   493 
   476 
   494       // ----- Look for first arg
   477       // ----- Look for first arg
   495       if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
   478       if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
   496         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
       
   497 
   479 
   498         // ----- Parse the options
   480         // ----- Parse the options
   499         $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
   481         $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
   500                                             array (PCLZIP_OPT_REMOVE_PATH => 'optional',
   482                                             array (PCLZIP_OPT_REMOVE_PATH => 'optional',
   501                                                    PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
   483                                                    PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
   510                                                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
   492                                                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
   511                                                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
   493                                                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
   512                                                    //, PCLZIP_OPT_CRYPT => 'optional'
   494                                                    //, PCLZIP_OPT_CRYPT => 'optional'
   513 												   ));
   495 												   ));
   514         if ($v_result != 1) {
   496         if ($v_result != 1) {
   515           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   516           return 0;
   497           return 0;
   517         }
   498         }
   518       }
   499       }
   519 
   500 
   520       // ----- Look for 2 args
   501       // ----- Look for 2 args
   521       // Here we need to support the first historic synopsis of the
   502       // Here we need to support the first historic synopsis of the
   522       // method.
   503       // method.
   523       else {
   504       else {
   524         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
       
   525 
   505 
   526         // ----- Get the first argument
   506         // ----- Get the first argument
   527         $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
   507         $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
   528 
   508 
   529         // ----- Look for the optional second argument
   509         // ----- Look for the optional second argument
   533         else if ($v_size > 2) {
   513         else if ($v_size > 2) {
   534           // ----- Error log
   514           // ----- Error log
   535           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
   515           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
   536 
   516 
   537           // ----- Return
   517           // ----- Return
   538           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
   539           return 0;
   518           return 0;
   540         }
   519         }
   541       }
   520       }
   542     }
   521     }
   543 
   522 
   547     // ----- Init
   526     // ----- Init
   548     $v_string_list = array();
   527     $v_string_list = array();
   549     $v_att_list = array();
   528     $v_att_list = array();
   550     $v_filedescr_list = array();
   529     $v_filedescr_list = array();
   551     $p_result_list = array();
   530     $p_result_list = array();
   552     
   531 
   553     // ----- Look if the $p_filelist is really an array
   532     // ----- Look if the $p_filelist is really an array
   554     if (is_array($p_filelist)) {
   533     if (is_array($p_filelist)) {
   555     
   534 
   556       // ----- Look if the first element is also an array
   535       // ----- Look if the first element is also an array
   557       //       This will mean that this is a file description entry
   536       //       This will mean that this is a file description entry
   558       if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
   537       if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
   559         $v_att_list = $p_filelist;
   538         $v_att_list = $p_filelist;
   560       }
   539       }
   561       
   540 
   562       // ----- The list is a list of string names
   541       // ----- The list is a list of string names
   563       else {
   542       else {
   564         $v_string_list = $p_filelist;
   543         $v_string_list = $p_filelist;
   565       }
   544       }
   566     }
   545     }
   572     }
   551     }
   573 
   552 
   574     // ----- Invalid variable type for $p_filelist
   553     // ----- Invalid variable type for $p_filelist
   575     else {
   554     else {
   576       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
   555       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
   577       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   578       return 0;
   556       return 0;
   579     }
   557     }
   580     
   558 
   581     // ----- Reformat the string list
   559     // ----- Reformat the string list
   582     if (sizeof($v_string_list) != 0) {
   560     if (sizeof($v_string_list) != 0) {
   583       foreach ($v_string_list as $v_string) {
   561       foreach ($v_string_list as $v_string) {
   584         $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
   562         $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
   585       }
   563       }
   586     }
   564     }
   587     
   565 
   588     // ----- For each file in the list check the attributes
   566     // ----- For each file in the list check the attributes
   589     $v_supported_attributes
   567     $v_supported_attributes
   590     = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
   568     = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
   591              ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
   569              ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
   592              ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
   570              ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
   598       $v_result = $this->privFileDescrParseAtt($v_entry,
   576       $v_result = $this->privFileDescrParseAtt($v_entry,
   599                                                $v_filedescr_list[],
   577                                                $v_filedescr_list[],
   600                                                $v_options,
   578                                                $v_options,
   601                                                $v_supported_attributes);
   579                                                $v_supported_attributes);
   602       if ($v_result != 1) {
   580       if ($v_result != 1) {
   603         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   604         return 0;
   581         return 0;
   605       }
   582       }
   606     }
   583     }
   607 
   584 
   608     // ----- Expand the filelist (expand directories)
   585     // ----- Expand the filelist (expand directories)
   609     $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
   586     $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
   610     if ($v_result != 1) {
   587     if ($v_result != 1) {
   611       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   612       return 0;
   588       return 0;
   613     }
   589     }
   614 
   590 
   615     // ----- Call the create fct
   591     // ----- Call the create fct
   616     $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
   592     $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
   617     if ($v_result != 1) {
   593     if ($v_result != 1) {
   618       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   619       return 0;
   594       return 0;
   620     }
   595     }
   621 
   596 
   622     // ----- Return
   597     // ----- Return
   623     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
       
   624     return $p_result_list;
   598     return $p_result_list;
   625   }
   599   }
   626   // --------------------------------------------------------------------------------
   600   // --------------------------------------------------------------------------------
   627 
   601 
   628   // --------------------------------------------------------------------------------
   602   // --------------------------------------------------------------------------------
   666   //   0 on an unrecoverable failure,
   640   //   0 on an unrecoverable failure,
   667   //   The list of the files in the archive.
   641   //   The list of the files in the archive.
   668   // --------------------------------------------------------------------------------
   642   // --------------------------------------------------------------------------------
   669   function listContent()
   643   function listContent()
   670   {
   644   {
   671     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::listContent', "");
       
   672     $v_result=1;
   645     $v_result=1;
   673 
   646 
   674     // ----- Reset the error handler
   647     // ----- Reset the error handler
   675     $this->privErrorReset();
   648     $this->privErrorReset();
   676 
   649 
   677     // ----- Check archive
   650     // ----- Check archive
   678     if (!$this->privCheckFormat()) {
   651     if (!$this->privCheckFormat()) {
   679       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   680       return(0);
   652       return(0);
   681     }
   653     }
   682 
   654 
   683     // ----- Call the extracting fct
   655     // ----- Call the extracting fct
   684     $p_list = array();
   656     $p_list = array();
   685     if (($v_result = $this->privList($p_list)) != 1)
   657     if (($v_result = $this->privList($p_list)) != 1)
   686     {
   658     {
   687       unset($p_list);
   659       unset($p_list);
   688       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
       
   689       return(0);
   660       return(0);
   690     }
   661     }
   691 
   662 
   692     // ----- Return
   663     // ----- Return
   693     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
       
   694     return $p_list;
   664     return $p_list;
   695   }
   665   }
   696   // --------------------------------------------------------------------------------
   666   // --------------------------------------------------------------------------------
   697 
   667 
   698   // --------------------------------------------------------------------------------
   668   // --------------------------------------------------------------------------------
   727   //   The list of the extracted files, with a status of the action.
   697   //   The list of the extracted files, with a status of the action.
   728   //   (see PclZip::listContent() for list entry format)
   698   //   (see PclZip::listContent() for list entry format)
   729   // --------------------------------------------------------------------------------
   699   // --------------------------------------------------------------------------------
   730   function extract()
   700   function extract()
   731   {
   701   {
   732     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extract", "");
       
   733     $v_result=1;
   702     $v_result=1;
   734 
   703 
   735     // ----- Reset the error handler
   704     // ----- Reset the error handler
   736     $this->privErrorReset();
   705     $this->privErrorReset();
   737 
   706 
   738     // ----- Check archive
   707     // ----- Check archive
   739     if (!$this->privCheckFormat()) {
   708     if (!$this->privCheckFormat()) {
   740       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   741       return(0);
   709       return(0);
   742     }
   710     }
   743 
   711 
   744     // ----- Set default values
   712     // ----- Set default values
   745     $v_options = array();
   713     $v_options = array();
   748     $v_remove_path = "";
   716     $v_remove_path = "";
   749     $v_remove_all_path = false;
   717     $v_remove_all_path = false;
   750 
   718 
   751     // ----- Look for variable options arguments
   719     // ----- Look for variable options arguments
   752     $v_size = func_num_args();
   720     $v_size = func_num_args();
   753     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
       
   754 
   721 
   755     // ----- Default values for option
   722     // ----- Default values for option
   756     $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
   723     $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
   757 
   724 
   758     // ----- Look for arguments
   725     // ----- Look for arguments
   760       // ----- Get the arguments
   727       // ----- Get the arguments
   761       $v_arg_list = func_get_args();
   728       $v_arg_list = func_get_args();
   762 
   729 
   763       // ----- Look for first arg
   730       // ----- Look for first arg
   764       if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
   731       if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
   765         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
       
   766 
   732 
   767         // ----- Parse the options
   733         // ----- Parse the options
   768         $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
   734         $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
   769                                             array (PCLZIP_OPT_PATH => 'optional',
   735                                             array (PCLZIP_OPT_PATH => 'optional',
   770                                                    PCLZIP_OPT_REMOVE_PATH => 'optional',
   736                                                    PCLZIP_OPT_REMOVE_PATH => 'optional',
   785                                                    PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
   751                                                    PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
   786                                                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
   752                                                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
   787                                                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
   753                                                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
   788 												    ));
   754 												    ));
   789         if ($v_result != 1) {
   755         if ($v_result != 1) {
   790           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   791           return 0;
   756           return 0;
   792         }
   757         }
   793 
   758 
   794         // ----- Set the arguments
   759         // ----- Set the arguments
   795         if (isset($v_options[PCLZIP_OPT_PATH])) {
   760         if (isset($v_options[PCLZIP_OPT_PATH])) {
   812 
   777 
   813       // ----- Look for 2 args
   778       // ----- Look for 2 args
   814       // Here we need to support the first historic synopsis of the
   779       // Here we need to support the first historic synopsis of the
   815       // method.
   780       // method.
   816       else {
   781       else {
   817         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
       
   818 
   782 
   819         // ----- Get the first argument
   783         // ----- Get the first argument
   820         $v_path = $v_arg_list[0];
   784         $v_path = $v_arg_list[0];
   821 
   785 
   822         // ----- Look for the optional second argument
   786         // ----- Look for the optional second argument
   826         else if ($v_size > 2) {
   790         else if ($v_size > 2) {
   827           // ----- Error log
   791           // ----- Error log
   828           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
   792           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
   829 
   793 
   830           // ----- Return
   794           // ----- Return
   831           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
       
   832           return 0;
   795           return 0;
   833         }
   796         }
   834       }
   797       }
   835     }
   798     }
   836 
   799 
   837     // ----- Look for default option values
   800     // ----- Look for default option values
   838     $this->privOptionDefaultThreshold($v_options);
   801     $this->privOptionDefaultThreshold($v_options);
   839 
   802 
   840     // ----- Trace
   803     // ----- Trace
   841     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
       
   842 
   804 
   843     // ----- Call the extracting fct
   805     // ----- Call the extracting fct
   844     $p_list = array();
   806     $p_list = array();
   845     $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
   807     $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
   846 	                                     $v_remove_all_path, $v_options);
   808 	                                     $v_remove_all_path, $v_options);
   847     if ($v_result < 1) {
   809     if ($v_result < 1) {
   848       unset($p_list);
   810       unset($p_list);
   849       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
       
   850       return(0);
   811       return(0);
   851     }
   812     }
   852 
   813 
   853     // ----- Return
   814     // ----- Return
   854     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
       
   855     return $p_list;
   815     return $p_list;
   856   }
   816   }
   857   // --------------------------------------------------------------------------------
   817   // --------------------------------------------------------------------------------
   858 
   818 
   859 
   819 
   894   //   (see PclZip::listContent() for list entry format)
   854   //   (see PclZip::listContent() for list entry format)
   895   // --------------------------------------------------------------------------------
   855   // --------------------------------------------------------------------------------
   896   //function extractByIndex($p_index, options...)
   856   //function extractByIndex($p_index, options...)
   897   function extractByIndex($p_index)
   857   function extractByIndex($p_index)
   898   {
   858   {
   899     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extractByIndex", "index='$p_index', ...");
       
   900     $v_result=1;
   859     $v_result=1;
   901 
   860 
   902     // ----- Reset the error handler
   861     // ----- Reset the error handler
   903     $this->privErrorReset();
   862     $this->privErrorReset();
   904 
   863 
   905     // ----- Check archive
   864     // ----- Check archive
   906     if (!$this->privCheckFormat()) {
   865     if (!$this->privCheckFormat()) {
   907       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   908       return(0);
   866       return(0);
   909     }
   867     }
   910 
   868 
   911     // ----- Set default values
   869     // ----- Set default values
   912     $v_options = array();
   870     $v_options = array();
   915     $v_remove_path = "";
   873     $v_remove_path = "";
   916     $v_remove_all_path = false;
   874     $v_remove_all_path = false;
   917 
   875 
   918     // ----- Look for variable options arguments
   876     // ----- Look for variable options arguments
   919     $v_size = func_num_args();
   877     $v_size = func_num_args();
   920     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
       
   921 
   878 
   922     // ----- Default values for option
   879     // ----- Default values for option
   923     $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
   880     $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
   924 
   881 
   925     // ----- Look for arguments
   882     // ----- Look for arguments
   931       array_shift($v_arg_list);
   888       array_shift($v_arg_list);
   932       $v_size--;
   889       $v_size--;
   933 
   890 
   934       // ----- Look for first arg
   891       // ----- Look for first arg
   935       if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
   892       if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
   936         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
       
   937 
   893 
   938         // ----- Parse the options
   894         // ----- Parse the options
   939         $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
   895         $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
   940                                             array (PCLZIP_OPT_PATH => 'optional',
   896                                             array (PCLZIP_OPT_PATH => 'optional',
   941                                                    PCLZIP_OPT_REMOVE_PATH => 'optional',
   897                                                    PCLZIP_OPT_REMOVE_PATH => 'optional',
   951                                                    PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
   907                                                    PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
   952                                                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
   908                                                    PCLZIP_OPT_TEMP_FILE_ON => 'optional',
   953                                                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
   909                                                    PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
   954 												   ));
   910 												   ));
   955         if ($v_result != 1) {
   911         if ($v_result != 1) {
   956           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
   957           return 0;
   912           return 0;
   958         }
   913         }
   959 
   914 
   960         // ----- Set the arguments
   915         // ----- Set the arguments
   961         if (isset($v_options[PCLZIP_OPT_PATH])) {
   916         if (isset($v_options[PCLZIP_OPT_PATH])) {
   974           }
   929           }
   975           $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
   930           $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
   976         }
   931         }
   977         if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
   932         if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
   978           $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
   933           $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
   979           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING not set.");
       
   980         }
   934         }
   981         else {
   935         else {
   982             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING set.");
       
   983         }
   936         }
   984       }
   937       }
   985 
   938 
   986       // ----- Look for 2 args
   939       // ----- Look for 2 args
   987       // Here we need to support the first historic synopsis of the
   940       // Here we need to support the first historic synopsis of the
   988       // method.
   941       // method.
   989       else {
   942       else {
   990         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
       
   991 
   943 
   992         // ----- Get the first argument
   944         // ----- Get the first argument
   993         $v_path = $v_arg_list[0];
   945         $v_path = $v_arg_list[0];
   994 
   946 
   995         // ----- Look for the optional second argument
   947         // ----- Look for the optional second argument
   999         else if ($v_size > 2) {
   951         else if ($v_size > 2) {
  1000           // ----- Error log
   952           // ----- Error log
  1001           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
   953           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  1002 
   954 
  1003           // ----- Return
   955           // ----- Return
  1004           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1005           return 0;
   956           return 0;
  1006         }
   957         }
  1007       }
   958       }
  1008     }
   959     }
  1009 
   960 
  1010     // ----- Trace
   961     // ----- Trace
  1011     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "index='$p_index', path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
       
  1012 
   962 
  1013     // ----- Trick
   963     // ----- Trick
  1014     // Here I want to reuse extractByRule(), so I need to parse the $p_index
   964     // Here I want to reuse extractByRule(), so I need to parse the $p_index
  1015     // with privParseOptions()
   965     // with privParseOptions()
  1016     $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
   966     $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
  1017     $v_options_trick = array();
   967     $v_options_trick = array();
  1018     $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
   968     $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
  1019                                         array (PCLZIP_OPT_BY_INDEX => 'optional' ));
   969                                         array (PCLZIP_OPT_BY_INDEX => 'optional' ));
  1020     if ($v_result != 1) {
   970     if ($v_result != 1) {
  1021         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
  1022         return 0;
   971         return 0;
  1023     }
   972     }
  1024     $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
   973     $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
  1025 
   974 
  1026     // ----- Look for default option values
   975     // ----- Look for default option values
  1027     $this->privOptionDefaultThreshold($v_options);
   976     $this->privOptionDefaultThreshold($v_options);
  1028 
   977 
  1029     // ----- Call the extracting fct
   978     // ----- Call the extracting fct
  1030     if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
   979     if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
  1031         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
       
  1032         return(0);
   980         return(0);
  1033     }
   981     }
  1034 
   982 
  1035     // ----- Return
   983     // ----- Return
  1036     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
       
  1037     return $p_list;
   984     return $p_list;
  1038   }
   985   }
  1039   // --------------------------------------------------------------------------------
   986   // --------------------------------------------------------------------------------
  1040 
   987 
  1041   // --------------------------------------------------------------------------------
   988   // --------------------------------------------------------------------------------
  1047   // Parameters :
   994   // Parameters :
  1048   //   None or optional arguments.
   995   //   None or optional arguments.
  1049   // Options :
   996   // Options :
  1050   //   PCLZIP_OPT_BY_INDEX :
   997   //   PCLZIP_OPT_BY_INDEX :
  1051   //   PCLZIP_OPT_BY_NAME :
   998   //   PCLZIP_OPT_BY_NAME :
  1052   //   PCLZIP_OPT_BY_EREG : 
   999   //   PCLZIP_OPT_BY_EREG :
  1053   //   PCLZIP_OPT_BY_PREG :
  1000   //   PCLZIP_OPT_BY_PREG :
  1054   // Return Values :
  1001   // Return Values :
  1055   //   0 on failure,
  1002   //   0 on failure,
  1056   //   The list of the files which are still present in the archive.
  1003   //   The list of the files which are still present in the archive.
  1057   //   (see PclZip::listContent() for list entry format)
  1004   //   (see PclZip::listContent() for list entry format)
  1058   // --------------------------------------------------------------------------------
  1005   // --------------------------------------------------------------------------------
  1059   function delete()
  1006   function delete()
  1060   {
  1007   {
  1061     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::delete", "");
       
  1062     $v_result=1;
  1008     $v_result=1;
  1063 
  1009 
  1064     // ----- Reset the error handler
  1010     // ----- Reset the error handler
  1065     $this->privErrorReset();
  1011     $this->privErrorReset();
  1066 
  1012 
  1067     // ----- Check archive
  1013     // ----- Check archive
  1068     if (!$this->privCheckFormat()) {
  1014     if (!$this->privCheckFormat()) {
  1069       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
  1070       return(0);
  1015       return(0);
  1071     }
  1016     }
  1072 
  1017 
  1073     // ----- Set default values
  1018     // ----- Set default values
  1074     $v_options = array();
  1019     $v_options = array();
  1075 
  1020 
  1076     // ----- Look for variable options arguments
  1021     // ----- Look for variable options arguments
  1077     $v_size = func_num_args();
  1022     $v_size = func_num_args();
  1078     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
       
  1079 
  1023 
  1080     // ----- Look for arguments
  1024     // ----- Look for arguments
  1081     if ($v_size > 0) {
  1025     if ($v_size > 0) {
  1082       // ----- Get the arguments
  1026       // ----- Get the arguments
  1083       $v_arg_list = func_get_args();
  1027       $v_arg_list = func_get_args();
  1087                                         array (PCLZIP_OPT_BY_NAME => 'optional',
  1031                                         array (PCLZIP_OPT_BY_NAME => 'optional',
  1088                                                PCLZIP_OPT_BY_EREG => 'optional',
  1032                                                PCLZIP_OPT_BY_EREG => 'optional',
  1089                                                PCLZIP_OPT_BY_PREG => 'optional',
  1033                                                PCLZIP_OPT_BY_PREG => 'optional',
  1090                                                PCLZIP_OPT_BY_INDEX => 'optional' ));
  1034                                                PCLZIP_OPT_BY_INDEX => 'optional' ));
  1091       if ($v_result != 1) {
  1035       if ($v_result != 1) {
  1092           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
  1093           return 0;
  1036           return 0;
  1094       }
  1037       }
  1095     }
  1038     }
  1096 
  1039 
  1097     // ----- Magic quotes trick
  1040     // ----- Magic quotes trick
  1100     // ----- Call the delete fct
  1043     // ----- Call the delete fct
  1101     $v_list = array();
  1044     $v_list = array();
  1102     if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
  1045     if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
  1103       $this->privSwapBackMagicQuotes();
  1046       $this->privSwapBackMagicQuotes();
  1104       unset($v_list);
  1047       unset($v_list);
  1105       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
       
  1106       return(0);
  1048       return(0);
  1107     }
  1049     }
  1108 
  1050 
  1109     // ----- Magic quotes trick
  1051     // ----- Magic quotes trick
  1110     $this->privSwapBackMagicQuotes();
  1052     $this->privSwapBackMagicQuotes();
  1111 
  1053 
  1112     // ----- Return
  1054     // ----- Return
  1113     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_list);
       
  1114     return $v_list;
  1055     return $v_list;
  1115   }
  1056   }
  1116   // --------------------------------------------------------------------------------
  1057   // --------------------------------------------------------------------------------
  1117 
  1058 
  1118   // --------------------------------------------------------------------------------
  1059   // --------------------------------------------------------------------------------
  1121   //   ***** Deprecated *****
  1062   //   ***** Deprecated *****
  1122   //   delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
  1063   //   delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
  1123   // --------------------------------------------------------------------------------
  1064   // --------------------------------------------------------------------------------
  1124   function deleteByIndex($p_index)
  1065   function deleteByIndex($p_index)
  1125   {
  1066   {
  1126     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::deleteByIndex", "index='$p_index'");
  1067 
  1127     
       
  1128     $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
  1068     $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
  1129 
  1069 
  1130     // ----- Return
  1070     // ----- Return
  1131     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
       
  1132     return $p_list;
  1071     return $p_list;
  1133   }
  1072   }
  1134   // --------------------------------------------------------------------------------
  1073   // --------------------------------------------------------------------------------
  1135 
  1074 
  1136   // --------------------------------------------------------------------------------
  1075   // --------------------------------------------------------------------------------
  1147   //   0 on failure,
  1086   //   0 on failure,
  1148   //   An array with the archive properties.
  1087   //   An array with the archive properties.
  1149   // --------------------------------------------------------------------------------
  1088   // --------------------------------------------------------------------------------
  1150   function properties()
  1089   function properties()
  1151   {
  1090   {
  1152     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::properties", "");
       
  1153 
  1091 
  1154     // ----- Reset the error handler
  1092     // ----- Reset the error handler
  1155     $this->privErrorReset();
  1093     $this->privErrorReset();
  1156 
  1094 
  1157     // ----- Magic quotes trick
  1095     // ----- Magic quotes trick
  1158     $this->privDisableMagicQuotes();
  1096     $this->privDisableMagicQuotes();
  1159 
  1097 
  1160     // ----- Check archive
  1098     // ----- Check archive
  1161     if (!$this->privCheckFormat()) {
  1099     if (!$this->privCheckFormat()) {
  1162       $this->privSwapBackMagicQuotes();
  1100       $this->privSwapBackMagicQuotes();
  1163       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
  1164       return(0);
  1101       return(0);
  1165     }
  1102     }
  1166 
  1103 
  1167     // ----- Default properties
  1104     // ----- Default properties
  1168     $v_prop = array();
  1105     $v_prop = array();
  1172 
  1109 
  1173     // ----- Look if file exists
  1110     // ----- Look if file exists
  1174     if (@is_file($this->zipname))
  1111     if (@is_file($this->zipname))
  1175     {
  1112     {
  1176       // ----- Open the zip file
  1113       // ----- Open the zip file
  1177       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  1178       if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  1114       if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  1179       {
  1115       {
  1180         $this->privSwapBackMagicQuotes();
  1116         $this->privSwapBackMagicQuotes();
  1181         
  1117 
  1182         // ----- Error log
  1118         // ----- Error log
  1183         PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  1119         PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  1184 
  1120 
  1185         // ----- Return
  1121         // ----- Return
  1186         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), 0);
       
  1187         return 0;
  1122         return 0;
  1188       }
  1123       }
  1189 
  1124 
  1190       // ----- Read the central directory informations
  1125       // ----- Read the central directory informations
  1191       $v_central_dir = array();
  1126       $v_central_dir = array();
  1192       if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  1127       if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  1193       {
  1128       {
  1194         $this->privSwapBackMagicQuotes();
  1129         $this->privSwapBackMagicQuotes();
  1195         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
  1196         return 0;
  1130         return 0;
  1197       }
  1131       }
  1198 
  1132 
  1199       // ----- Close the zip file
  1133       // ----- Close the zip file
  1200       $this->privCloseFd();
  1134       $this->privCloseFd();
  1207 
  1141 
  1208     // ----- Magic quotes trick
  1142     // ----- Magic quotes trick
  1209     $this->privSwapBackMagicQuotes();
  1143     $this->privSwapBackMagicQuotes();
  1210 
  1144 
  1211     // ----- Return
  1145     // ----- Return
  1212     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_prop);
       
  1213     return $v_prop;
  1146     return $v_prop;
  1214   }
  1147   }
  1215   // --------------------------------------------------------------------------------
  1148   // --------------------------------------------------------------------------------
  1216 
  1149 
  1217   // --------------------------------------------------------------------------------
  1150   // --------------------------------------------------------------------------------
  1226   //   1 on success.
  1159   //   1 on success.
  1227   //   0 or a negative value on error (error code).
  1160   //   0 or a negative value on error (error code).
  1228   // --------------------------------------------------------------------------------
  1161   // --------------------------------------------------------------------------------
  1229   function duplicate($p_archive)
  1162   function duplicate($p_archive)
  1230   {
  1163   {
  1231     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::duplicate", "");
       
  1232     $v_result = 1;
  1164     $v_result = 1;
  1233 
  1165 
  1234     // ----- Reset the error handler
  1166     // ----- Reset the error handler
  1235     $this->privErrorReset();
  1167     $this->privErrorReset();
  1236 
  1168 
  1237     // ----- Look if the $p_archive is a PclZip object
  1169     // ----- Look if the $p_archive is a PclZip object
  1238     if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
  1170     if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
  1239     {
  1171     {
  1240       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is valid PclZip object '".$p_archive->zipname."'");
       
  1241 
  1172 
  1242       // ----- Duplicate the archive
  1173       // ----- Duplicate the archive
  1243       $v_result = $this->privDuplicate($p_archive->zipname);
  1174       $v_result = $this->privDuplicate($p_archive->zipname);
  1244     }
  1175     }
  1245 
  1176 
  1246     // ----- Look if the $p_archive is a string (so a filename)
  1177     // ----- Look if the $p_archive is a string (so a filename)
  1247     else if (is_string($p_archive))
  1178     else if (is_string($p_archive))
  1248     {
  1179     {
  1249       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is a filename '$p_archive'");
       
  1250 
  1180 
  1251       // ----- Check that $p_archive is a valid zip file
  1181       // ----- Check that $p_archive is a valid zip file
  1252       // TBC : Should also check the archive format
  1182       // TBC : Should also check the archive format
  1253       if (!is_file($p_archive)) {
  1183       if (!is_file($p_archive)) {
  1254         // ----- Error log
  1184         // ----- Error log
  1268       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1198       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1269       $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1199       $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1270     }
  1200     }
  1271 
  1201 
  1272     // ----- Return
  1202     // ----- Return
  1273     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  1274     return $v_result;
  1203     return $v_result;
  1275   }
  1204   }
  1276   // --------------------------------------------------------------------------------
  1205   // --------------------------------------------------------------------------------
  1277 
  1206 
  1278   // --------------------------------------------------------------------------------
  1207   // --------------------------------------------------------------------------------
  1289   //   1 on success,
  1218   //   1 on success,
  1290   //   0 or negative values on error (see below).
  1219   //   0 or negative values on error (see below).
  1291   // --------------------------------------------------------------------------------
  1220   // --------------------------------------------------------------------------------
  1292   function merge($p_archive_to_add)
  1221   function merge($p_archive_to_add)
  1293   {
  1222   {
  1294     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::merge", "");
       
  1295     $v_result = 1;
  1223     $v_result = 1;
  1296 
  1224 
  1297     // ----- Reset the error handler
  1225     // ----- Reset the error handler
  1298     $this->privErrorReset();
  1226     $this->privErrorReset();
  1299 
  1227 
  1300     // ----- Check archive
  1228     // ----- Check archive
  1301     if (!$this->privCheckFormat()) {
  1229     if (!$this->privCheckFormat()) {
  1302       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
       
  1303       return(0);
  1230       return(0);
  1304     }
  1231     }
  1305 
  1232 
  1306     // ----- Look if the $p_archive_to_add is a PclZip object
  1233     // ----- Look if the $p_archive_to_add is a PclZip object
  1307     if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
  1234     if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
  1308     {
  1235     {
  1309       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is valid PclZip object");
       
  1310 
  1236 
  1311       // ----- Merge the archive
  1237       // ----- Merge the archive
  1312       $v_result = $this->privMerge($p_archive_to_add);
  1238       $v_result = $this->privMerge($p_archive_to_add);
  1313     }
  1239     }
  1314 
  1240 
  1315     // ----- Look if the $p_archive_to_add is a string (so a filename)
  1241     // ----- Look if the $p_archive_to_add is a string (so a filename)
  1316     else if (is_string($p_archive_to_add))
  1242     else if (is_string($p_archive_to_add))
  1317     {
  1243     {
  1318       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is a filename");
       
  1319 
  1244 
  1320       // ----- Create a temporary archive
  1245       // ----- Create a temporary archive
  1321       $v_object_archive = new PclZip($p_archive_to_add);
  1246       $v_object_archive = new PclZip($p_archive_to_add);
  1322 
  1247 
  1323       // ----- Merge the archive
  1248       // ----- Merge the archive
  1331       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1256       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1332       $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1257       $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1333     }
  1258     }
  1334 
  1259 
  1335     // ----- Return
  1260     // ----- Return
  1336     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  1337     return $v_result;
  1261     return $v_result;
  1338   }
  1262   }
  1339   // --------------------------------------------------------------------------------
  1263   // --------------------------------------------------------------------------------
  1340 
  1264 
  1341 
  1265 
  1446   //   true on success,
  1370   //   true on success,
  1447   //   false on error, the error code is set.
  1371   //   false on error, the error code is set.
  1448   // --------------------------------------------------------------------------------
  1372   // --------------------------------------------------------------------------------
  1449   function privCheckFormat($p_level=0)
  1373   function privCheckFormat($p_level=0)
  1450   {
  1374   {
  1451     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFormat", "");
       
  1452     $v_result = true;
  1375     $v_result = true;
  1453 
  1376 
  1454 	// ----- Reset the file system cache
  1377 	// ----- Reset the file system cache
  1455     clearstatcache();
  1378     clearstatcache();
  1456 
  1379 
  1459 
  1382 
  1460     // ----- Look if the file exits
  1383     // ----- Look if the file exits
  1461     if (!is_file($this->zipname)) {
  1384     if (!is_file($this->zipname)) {
  1462       // ----- Error log
  1385       // ----- Error log
  1463       PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
  1386       PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
  1464       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
       
  1465       return(false);
  1387       return(false);
  1466     }
  1388     }
  1467 
  1389 
  1468     // ----- Check that the file is readeable
  1390     // ----- Check that the file is readeable
  1469     if (!is_readable($this->zipname)) {
  1391     if (!is_readable($this->zipname)) {
  1470       // ----- Error log
  1392       // ----- Error log
  1471       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
  1393       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
  1472       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
       
  1473       return(false);
  1394       return(false);
  1474     }
  1395     }
  1475 
  1396 
  1476     // ----- Check the magic code
  1397     // ----- Check the magic code
  1477     // TBC
  1398     // TBC
  1481 
  1402 
  1482     // ----- Check each file header
  1403     // ----- Check each file header
  1483     // TBC
  1404     // TBC
  1484 
  1405 
  1485     // ----- Return
  1406     // ----- Return
  1486     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  1487     return $v_result;
  1407     return $v_result;
  1488   }
  1408   }
  1489   // --------------------------------------------------------------------------------
  1409   // --------------------------------------------------------------------------------
  1490 
  1410 
  1491   // --------------------------------------------------------------------------------
  1411   // --------------------------------------------------------------------------------
  1503   //   1 on success.
  1423   //   1 on success.
  1504   //   0 on failure.
  1424   //   0 on failure.
  1505   // --------------------------------------------------------------------------------
  1425   // --------------------------------------------------------------------------------
  1506   function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
  1426   function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
  1507   {
  1427   {
  1508     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privParseOptions", "");
       
  1509     $v_result=1;
  1428     $v_result=1;
  1510     
  1429 
  1511     // ----- Read the options
  1430     // ----- Read the options
  1512     $i=0;
  1431     $i=0;
  1513     while ($i<$p_size) {
  1432     while ($i<$p_size) {
  1514       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Looking for table index $i, option = '".PclZipUtilOptionText($p_options_list[$i])."(".$p_options_list[$i].")'");
       
  1515 
  1433 
  1516       // ----- Check if the option is supported
  1434       // ----- Check if the option is supported
  1517       if (!isset($v_requested_options[$p_options_list[$i]])) {
  1435       if (!isset($v_requested_options[$p_options_list[$i]])) {
  1518         // ----- Error log
  1436         // ----- Error log
  1519         PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
  1437         PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
  1520 
  1438 
  1521         // ----- Return
  1439         // ----- Return
  1522         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1523         return PclZip::errorCode();
  1440         return PclZip::errorCode();
  1524       }
  1441       }
  1525 
  1442 
  1526       // ----- Look for next option
  1443       // ----- Look for next option
  1527       switch ($p_options_list[$i]) {
  1444       switch ($p_options_list[$i]) {
  1533           if (($i+1) >= $p_size) {
  1450           if (($i+1) >= $p_size) {
  1534             // ----- Error log
  1451             // ----- Error log
  1535             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1452             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1536 
  1453 
  1537             // ----- Return
  1454             // ----- Return
  1538             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1539             return PclZip::errorCode();
  1455             return PclZip::errorCode();
  1540           }
  1456           }
  1541 
  1457 
  1542           // ----- Get the value
  1458           // ----- Get the value
  1543           $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
  1459           $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
  1544           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1545           $i++;
  1460           $i++;
  1546         break;
  1461         break;
  1547 
  1462 
  1548         case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
  1463         case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
  1549           // ----- Check the number of parameters
  1464           // ----- Check the number of parameters
  1550           if (($i+1) >= $p_size) {
  1465           if (($i+1) >= $p_size) {
  1551             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1466             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1552             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1553             return PclZip::errorCode();
  1467             return PclZip::errorCode();
  1554           }
  1468           }
  1555           
  1469 
  1556           // ----- Check for incompatible options
  1470           // ----- Check for incompatible options
  1557           if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1471           if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1558             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1472             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1559             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1560             return PclZip::errorCode();
  1473             return PclZip::errorCode();
  1561           }
  1474           }
  1562           
  1475 
  1563           // ----- Check the value
  1476           // ----- Check the value
  1564           $v_value = $p_options_list[$i+1];
  1477           $v_value = $p_options_list[$i+1];
  1565           if ((!is_integer($v_value)) || ($v_value<0)) {
  1478           if ((!is_integer($v_value)) || ($v_value<0)) {
  1566             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1479             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1567             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1568             return PclZip::errorCode();
  1480             return PclZip::errorCode();
  1569           }
  1481           }
  1570 
  1482 
  1571           // ----- Get the value (and convert it in bytes)
  1483           // ----- Get the value (and convert it in bytes)
  1572           $v_result_list[$p_options_list[$i]] = $v_value*1048576;
  1484           $v_result_list[$p_options_list[$i]] = $v_value*1048576;
  1573           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1574           $i++;
  1485           $i++;
  1575         break;
  1486         break;
  1576 
  1487 
  1577         case PCLZIP_OPT_TEMP_FILE_ON :
  1488         case PCLZIP_OPT_TEMP_FILE_ON :
  1578           // ----- Check for incompatible options
  1489           // ----- Check for incompatible options
  1579           if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1490           if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1580             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1491             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1581             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1582             return PclZip::errorCode();
  1492             return PclZip::errorCode();
  1583           }
  1493           }
  1584           
  1494 
  1585           $v_result_list[$p_options_list[$i]] = true;
  1495           $v_result_list[$p_options_list[$i]] = true;
  1586           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1587         break;
  1496         break;
  1588 
  1497 
  1589         case PCLZIP_OPT_TEMP_FILE_OFF :
  1498         case PCLZIP_OPT_TEMP_FILE_OFF :
  1590           // ----- Check for incompatible options
  1499           // ----- Check for incompatible options
  1591           if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
  1500           if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
  1592             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
  1501             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
  1593             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1594             return PclZip::errorCode();
  1502             return PclZip::errorCode();
  1595           }
  1503           }
  1596           // ----- Check for incompatible options
  1504           // ----- Check for incompatible options
  1597           if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1505           if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1598             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
  1506             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
  1599             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1600             return PclZip::errorCode();
  1507             return PclZip::errorCode();
  1601           }
  1508           }
  1602           
  1509 
  1603           $v_result_list[$p_options_list[$i]] = true;
  1510           $v_result_list[$p_options_list[$i]] = true;
  1604           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1605         break;
  1511         break;
  1606 
  1512 
  1607         case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
  1513         case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
  1608           // ----- Check the number of parameters
  1514           // ----- Check the number of parameters
  1609           if (($i+1) >= $p_size) {
  1515           if (($i+1) >= $p_size) {
  1610             // ----- Error log
  1516             // ----- Error log
  1611             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1517             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1612 
  1518 
  1613             // ----- Return
  1519             // ----- Return
  1614             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1615             return PclZip::errorCode();
  1520             return PclZip::errorCode();
  1616           }
  1521           }
  1617 
  1522 
  1618           // ----- Get the value
  1523           // ----- Get the value
  1619           if (   is_string($p_options_list[$i+1])
  1524           if (   is_string($p_options_list[$i+1])
  1620               && ($p_options_list[$i+1] != '')) {
  1525               && ($p_options_list[$i+1] != '')) {
  1621             $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
  1526             $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
  1622             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1623             $i++;
  1527             $i++;
  1624           }
  1528           }
  1625           else {
  1529           else {
  1626             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." set with an empty value is ignored.");
       
  1627           }
  1530           }
  1628         break;
  1531         break;
  1629 
  1532 
  1630         // ----- Look for options that request an array of string for value
  1533         // ----- Look for options that request an array of string for value
  1631         case PCLZIP_OPT_BY_NAME :
  1534         case PCLZIP_OPT_BY_NAME :
  1633           if (($i+1) >= $p_size) {
  1536           if (($i+1) >= $p_size) {
  1634             // ----- Error log
  1537             // ----- Error log
  1635             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1538             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1636 
  1539 
  1637             // ----- Return
  1540             // ----- Return
  1638             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1639             return PclZip::errorCode();
  1541             return PclZip::errorCode();
  1640           }
  1542           }
  1641 
  1543 
  1642           // ----- Get the value
  1544           // ----- Get the value
  1643           if (is_string($p_options_list[$i+1])) {
  1545           if (is_string($p_options_list[$i+1])) {
  1649           else {
  1551           else {
  1650             // ----- Error log
  1552             // ----- Error log
  1651             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1553             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1652 
  1554 
  1653             // ----- Return
  1555             // ----- Return
  1654             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1655             return PclZip::errorCode();
  1556             return PclZip::errorCode();
  1656           }
  1557           }
  1657           ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1658           $i++;
  1558           $i++;
  1659         break;
  1559         break;
  1660 
  1560 
  1661         // ----- Look for options that request an EREG or PREG expression
  1561         // ----- Look for options that request an EREG or PREG expression
  1662         case PCLZIP_OPT_BY_EREG :
  1562         case PCLZIP_OPT_BY_EREG :
       
  1563           // ereg() is deprecated starting with PHP 5.3. Move PCLZIP_OPT_BY_EREG
       
  1564           // to PCLZIP_OPT_BY_PREG
       
  1565           $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
  1663         case PCLZIP_OPT_BY_PREG :
  1566         case PCLZIP_OPT_BY_PREG :
  1664         //case PCLZIP_OPT_CRYPT :
  1567         //case PCLZIP_OPT_CRYPT :
  1665           // ----- Check the number of parameters
  1568           // ----- Check the number of parameters
  1666           if (($i+1) >= $p_size) {
  1569           if (($i+1) >= $p_size) {
  1667             // ----- Error log
  1570             // ----- Error log
  1668             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1571             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1669 
  1572 
  1670             // ----- Return
  1573             // ----- Return
  1671             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1672             return PclZip::errorCode();
  1574             return PclZip::errorCode();
  1673           }
  1575           }
  1674 
  1576 
  1675           // ----- Get the value
  1577           // ----- Get the value
  1676           if (is_string($p_options_list[$i+1])) {
  1578           if (is_string($p_options_list[$i+1])) {
  1679           else {
  1581           else {
  1680             // ----- Error log
  1582             // ----- Error log
  1681             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1583             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1682 
  1584 
  1683             // ----- Return
  1585             // ----- Return
  1684             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1685             return PclZip::errorCode();
  1586             return PclZip::errorCode();
  1686           }
  1587           }
  1687           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1688           $i++;
  1588           $i++;
  1689         break;
  1589         break;
  1690 
  1590 
  1691         // ----- Look for options that takes a string
  1591         // ----- Look for options that takes a string
  1692         case PCLZIP_OPT_COMMENT :
  1592         case PCLZIP_OPT_COMMENT :
  1699 			                     "Missing parameter value for option '"
  1599 			                     "Missing parameter value for option '"
  1700 								 .PclZipUtilOptionText($p_options_list[$i])
  1600 								 .PclZipUtilOptionText($p_options_list[$i])
  1701 								 ."'");
  1601 								 ."'");
  1702 
  1602 
  1703             // ----- Return
  1603             // ----- Return
  1704             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1705             return PclZip::errorCode();
  1604             return PclZip::errorCode();
  1706           }
  1605           }
  1707 
  1606 
  1708           // ----- Get the value
  1607           // ----- Get the value
  1709           if (is_string($p_options_list[$i+1])) {
  1608           if (is_string($p_options_list[$i+1])) {
  1715 			                     "Wrong parameter value for option '"
  1614 			                     "Wrong parameter value for option '"
  1716 								 .PclZipUtilOptionText($p_options_list[$i])
  1615 								 .PclZipUtilOptionText($p_options_list[$i])
  1717 								 ."'");
  1616 								 ."'");
  1718 
  1617 
  1719             // ----- Return
  1618             // ----- Return
  1720             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1721             return PclZip::errorCode();
  1619             return PclZip::errorCode();
  1722           }
  1620           }
  1723           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1724           $i++;
  1621           $i++;
  1725         break;
  1622         break;
  1726 
  1623 
  1727         // ----- Look for options that request an array of index
  1624         // ----- Look for options that request an array of index
  1728         case PCLZIP_OPT_BY_INDEX :
  1625         case PCLZIP_OPT_BY_INDEX :
  1730           if (($i+1) >= $p_size) {
  1627           if (($i+1) >= $p_size) {
  1731             // ----- Error log
  1628             // ----- Error log
  1732             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1629             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1733 
  1630 
  1734             // ----- Return
  1631             // ----- Return
  1735             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1736             return PclZip::errorCode();
  1632             return PclZip::errorCode();
  1737           }
  1633           }
  1738 
  1634 
  1739           // ----- Get the value
  1635           // ----- Get the value
  1740           $v_work_list = array();
  1636           $v_work_list = array();
  1741           if (is_string($p_options_list[$i+1])) {
  1637           if (is_string($p_options_list[$i+1])) {
  1742               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is a string '".$p_options_list[$i+1]."'");
       
  1743 
  1638 
  1744               // ----- Remove spaces
  1639               // ----- Remove spaces
  1745               $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
  1640               $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
  1746 
  1641 
  1747               // ----- Parse items
  1642               // ----- Parse items
  1748               $v_work_list = explode(",", $p_options_list[$i+1]);
  1643               $v_work_list = explode(",", $p_options_list[$i+1]);
  1749           }
  1644           }
  1750           else if (is_integer($p_options_list[$i+1])) {
  1645           else if (is_integer($p_options_list[$i+1])) {
  1751               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an integer '".$p_options_list[$i+1]."'");
       
  1752               $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
  1646               $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
  1753           }
  1647           }
  1754           else if (is_array($p_options_list[$i+1])) {
  1648           else if (is_array($p_options_list[$i+1])) {
  1755               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an array");
       
  1756               $v_work_list = $p_options_list[$i+1];
  1649               $v_work_list = $p_options_list[$i+1];
  1757           }
  1650           }
  1758           else {
  1651           else {
  1759             // ----- Error log
  1652             // ----- Error log
  1760             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1653             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1761 
  1654 
  1762             // ----- Return
  1655             // ----- Return
  1763             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1764             return PclZip::errorCode();
  1656             return PclZip::errorCode();
  1765           }
  1657           }
  1766           
  1658 
  1767           // ----- Reduce the index list
  1659           // ----- Reduce the index list
  1768           // each index item in the list must be a couple with a start and
  1660           // each index item in the list must be a couple with a start and
  1769           // an end value : [0,3], [5-5], [8-10], ...
  1661           // an end value : [0,3], [5-5], [8-10], ...
  1770           // ----- Check the format of each item
  1662           // ----- Check the format of each item
  1771           $v_sort_flag=false;
  1663           $v_sort_flag=false;
  1772           $v_sort_value=0;
  1664           $v_sort_value=0;
  1773           for ($j=0; $j<sizeof($v_work_list); $j++) {
  1665           for ($j=0; $j<sizeof($v_work_list); $j++) {
  1774               // ----- Explode the item
  1666               // ----- Explode the item
  1775               $v_item_list = explode("-", $v_work_list[$j]);
  1667               $v_item_list = explode("-", $v_work_list[$j]);
  1776               $v_size_item_list = sizeof($v_item_list);
  1668               $v_size_item_list = sizeof($v_item_list);
  1777               
  1669 
  1778               // ----- TBC : Here we might check that each item is a
  1670               // ----- TBC : Here we might check that each item is a
  1779               // real integer ...
  1671               // real integer ...
  1780               
  1672 
  1781               // ----- Look for single value
  1673               // ----- Look for single value
  1782               if ($v_size_item_list == 1) {
  1674               if ($v_size_item_list == 1) {
  1783                   // ----- Set the option value
  1675                   // ----- Set the option value
  1784                   $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  1676                   $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  1785                   $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
  1677                   $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
  1792               else {
  1684               else {
  1793                   // ----- Error log
  1685                   // ----- Error log
  1794                   PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1686                   PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1795 
  1687 
  1796                   // ----- Return
  1688                   // ----- Return
  1797                   //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1798                   return PclZip::errorCode();
  1689                   return PclZip::errorCode();
  1799               }
  1690               }
  1800 
  1691 
  1801               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extracted index item = [".$v_result_list[$p_options_list[$i]][$j]['start'].",".$v_result_list[$p_options_list[$i]][$j]['end']."]");
       
  1802 
  1692 
  1803               // ----- Look for list sort
  1693               // ----- Look for list sort
  1804               if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
  1694               if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
  1805                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The list should be sorted ...");
       
  1806                   $v_sort_flag=true;
  1695                   $v_sort_flag=true;
  1807 
  1696 
  1808                   // ----- TBC : An automatic sort should be writen ...
  1697                   // ----- TBC : An automatic sort should be writen ...
  1809                   // ----- Error log
  1698                   // ----- Error log
  1810                   PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1699                   PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1811 
  1700 
  1812                   // ----- Return
  1701                   // ----- Return
  1813                   //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1814                   return PclZip::errorCode();
  1702                   return PclZip::errorCode();
  1815               }
  1703               }
  1816               $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
  1704               $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
  1817           }
  1705           }
  1818           
  1706 
  1819           // ----- Sort the items
  1707           // ----- Sort the items
  1820           if ($v_sort_flag) {
  1708           if ($v_sort_flag) {
  1821               // TBC : To Be Completed
  1709               // TBC : To Be Completed
  1822               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "List sorting is not yet write ...");
       
  1823           }
  1710           }
  1824 
  1711 
  1825           // ----- Next option
  1712           // ----- Next option
  1826           $i++;
  1713           $i++;
  1827         break;
  1714         break;
  1832         case PCLZIP_OPT_NO_COMPRESSION :
  1719         case PCLZIP_OPT_NO_COMPRESSION :
  1833         case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
  1720         case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
  1834         case PCLZIP_OPT_REPLACE_NEWER :
  1721         case PCLZIP_OPT_REPLACE_NEWER :
  1835         case PCLZIP_OPT_STOP_ON_ERROR :
  1722         case PCLZIP_OPT_STOP_ON_ERROR :
  1836           $v_result_list[$p_options_list[$i]] = true;
  1723           $v_result_list[$p_options_list[$i]] = true;
  1837           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1838         break;
  1724         break;
  1839 
  1725 
  1840         // ----- Look for options that request an octal value
  1726         // ----- Look for options that request an octal value
  1841         case PCLZIP_OPT_SET_CHMOD :
  1727         case PCLZIP_OPT_SET_CHMOD :
  1842           // ----- Check the number of parameters
  1728           // ----- Check the number of parameters
  1843           if (($i+1) >= $p_size) {
  1729           if (($i+1) >= $p_size) {
  1844             // ----- Error log
  1730             // ----- Error log
  1845             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1731             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1846 
  1732 
  1847             // ----- Return
  1733             // ----- Return
  1848             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1849             return PclZip::errorCode();
  1734             return PclZip::errorCode();
  1850           }
  1735           }
  1851 
  1736 
  1852           // ----- Get the value
  1737           // ----- Get the value
  1853           $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1738           $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1854           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
       
  1855           $i++;
  1739           $i++;
  1856         break;
  1740         break;
  1857 
  1741 
  1858         // ----- Look for options that request a call-back
  1742         // ----- Look for options that request a call-back
  1859         case PCLZIP_CB_PRE_EXTRACT :
  1743         case PCLZIP_CB_PRE_EXTRACT :
  1870           if (($i+1) >= $p_size) {
  1754           if (($i+1) >= $p_size) {
  1871             // ----- Error log
  1755             // ----- Error log
  1872             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1756             PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1873 
  1757 
  1874             // ----- Return
  1758             // ----- Return
  1875             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1876             return PclZip::errorCode();
  1759             return PclZip::errorCode();
  1877           }
  1760           }
  1878 
  1761 
  1879           // ----- Get the value
  1762           // ----- Get the value
  1880           $v_function_name = $p_options_list[$i+1];
  1763           $v_function_name = $p_options_list[$i+1];
  1881           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "call-back ".PclZipUtilOptionText($p_options_list[$i])." = '".$v_function_name."'");
       
  1882 
  1764 
  1883           // ----- Check that the value is a valid existing function
  1765           // ----- Check that the value is a valid existing function
  1884           if (!function_exists($v_function_name)) {
  1766           if (!function_exists($v_function_name)) {
  1885             // ----- Error log
  1767             // ----- Error log
  1886             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1768             PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1887 
  1769 
  1888             // ----- Return
  1770             // ----- Return
  1889             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1890             return PclZip::errorCode();
  1771             return PclZip::errorCode();
  1891           }
  1772           }
  1892 
  1773 
  1893           // ----- Set the attribute
  1774           // ----- Set the attribute
  1894           $v_result_list[$p_options_list[$i]] = $v_function_name;
  1775           $v_result_list[$p_options_list[$i]] = $v_function_name;
  1900           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  1781           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  1901 		                       "Unknown parameter '"
  1782 		                       "Unknown parameter '"
  1902 							   .$p_options_list[$i]."'");
  1783 							   .$p_options_list[$i]."'");
  1903 
  1784 
  1904           // ----- Return
  1785           // ----- Return
  1905           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1906           return PclZip::errorCode();
  1786           return PclZip::errorCode();
  1907       }
  1787       }
  1908 
  1788 
  1909       // ----- Next options
  1789       // ----- Next options
  1910       $i++;
  1790       $i++;
  1913     // ----- Look for mandatory options
  1793     // ----- Look for mandatory options
  1914     if ($v_requested_options !== false) {
  1794     if ($v_requested_options !== false) {
  1915       for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  1795       for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  1916         // ----- Look for mandatory option
  1796         // ----- Look for mandatory option
  1917         if ($v_requested_options[$key] == 'mandatory') {
  1797         if ($v_requested_options[$key] == 'mandatory') {
  1918           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
       
  1919           // ----- Look if present
  1798           // ----- Look if present
  1920           if (!isset($v_result_list[$key])) {
  1799           if (!isset($v_result_list[$key])) {
  1921             // ----- Error log
  1800             // ----- Error log
  1922             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  1801             PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  1923 
  1802 
  1924             // ----- Return
  1803             // ----- Return
  1925             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  1926             return PclZip::errorCode();
  1804             return PclZip::errorCode();
  1927           }
  1805           }
  1928         }
  1806         }
  1929       }
  1807       }
  1930     }
  1808     }
  1931     
  1809 
  1932     // ----- Look for default values
  1810     // ----- Look for default values
  1933     if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1811     if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1934       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Calculate auto threshold");
  1812 
  1935       
       
  1936     }
  1813     }
  1937 
  1814 
  1938     // ----- Return
  1815     // ----- Return
  1939     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  1940     return $v_result;
  1816     return $v_result;
  1941   }
  1817   }
  1942   // --------------------------------------------------------------------------------
  1818   // --------------------------------------------------------------------------------
  1943 
  1819 
  1944   // --------------------------------------------------------------------------------
  1820   // --------------------------------------------------------------------------------
  1947   // Parameters :
  1823   // Parameters :
  1948   // Return Values :
  1824   // Return Values :
  1949   // --------------------------------------------------------------------------------
  1825   // --------------------------------------------------------------------------------
  1950   function privOptionDefaultThreshold(&$p_options)
  1826   function privOptionDefaultThreshold(&$p_options)
  1951   {
  1827   {
  1952     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privOptionDefaultThreshold", "");
       
  1953     $v_result=1;
  1828     $v_result=1;
  1954     
  1829 
  1955     if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  1830     if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  1956         || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1831         || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1957       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  1958       return $v_result;
  1832       return $v_result;
  1959     }
  1833     }
  1960     
  1834 
  1961     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Create an auto-threshold for use of temporay files");
       
  1962     // ----- Get 'memory_limit' configuration value
  1835     // ----- Get 'memory_limit' configuration value
  1963     $v_memory_limit = ini_get('memory_limit');
  1836     $v_memory_limit = ini_get('memory_limit');
  1964     $v_memory_limit = trim($v_memory_limit);
  1837     $v_memory_limit = trim($v_memory_limit);
  1965     $last = strtolower(substr($v_memory_limit, -1));
  1838     $last = strtolower(substr($v_memory_limit, -1));
  1966  
  1839 
  1967     if($last == 'g')
  1840     if($last == 'g')
  1968         //$v_memory_limit = $v_memory_limit*1024*1024*1024;
  1841         //$v_memory_limit = $v_memory_limit*1024*1024*1024;
  1969         $v_memory_limit = $v_memory_limit*1073741824;
  1842         $v_memory_limit = $v_memory_limit*1073741824;
  1970     if($last == 'm')
  1843     if($last == 'm')
  1971         //$v_memory_limit = $v_memory_limit*1024*1024;
  1844         //$v_memory_limit = $v_memory_limit*1024*1024;
  1972         $v_memory_limit = $v_memory_limit*1048576;
  1845         $v_memory_limit = $v_memory_limit*1048576;
  1973     if($last == 'k')
  1846     if($last == 'k')
  1974         $v_memory_limit = $v_memory_limit*1024;
  1847         $v_memory_limit = $v_memory_limit*1024;
  1975             
  1848 
  1976     $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit*PCLZIP_TEMPORARY_FILE_RATIO);
  1849     $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit*PCLZIP_TEMPORARY_FILE_RATIO);
  1977     
  1850 
  1978     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Current memory usage : ".memory_get_usage(TRUE)." bytes");
       
  1979     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Threshold value is : ".$p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]." bytes");
       
  1980 
  1851 
  1981     // ----- Sanity check : No threshold if value lower than 1M
  1852     // ----- Sanity check : No threshold if value lower than 1M
  1982     if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
  1853     if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
  1983       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Unset the threshold (value ".$p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD].") because under 1Mo sanity check)");
       
  1984       unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
  1854       unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
  1985     }
  1855     }
  1986           
  1856 
  1987     // ----- Return
  1857     // ----- Return
  1988     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  1989     return $v_result;
  1858     return $v_result;
  1990   }
  1859   }
  1991   // --------------------------------------------------------------------------------
  1860   // --------------------------------------------------------------------------------
  1992 
  1861 
  1993   // --------------------------------------------------------------------------------
  1862   // --------------------------------------------------------------------------------
  1998   //   1 on success.
  1867   //   1 on success.
  1999   //   0 on failure.
  1868   //   0 on failure.
  2000   // --------------------------------------------------------------------------------
  1869   // --------------------------------------------------------------------------------
  2001   function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
  1870   function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
  2002   {
  1871   {
  2003     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrParseAtt", "");
       
  2004     $v_result=1;
  1872     $v_result=1;
  2005     
  1873 
  2006     // ----- For each file in the list check the attributes
  1874     // ----- For each file in the list check the attributes
  2007     foreach ($p_file_list as $v_key => $v_value) {
  1875     foreach ($p_file_list as $v_key => $v_value) {
  2008     
  1876 
  2009       // ----- Check if the option is supported
  1877       // ----- Check if the option is supported
  2010       if (!isset($v_requested_options[$v_key])) {
  1878       if (!isset($v_requested_options[$v_key])) {
  2011         // ----- Error log
  1879         // ----- Error log
  2012         PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
  1880         PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
  2013 
  1881 
  2014         // ----- Return
  1882         // ----- Return
  2015         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2016         return PclZip::errorCode();
  1883         return PclZip::errorCode();
  2017       }
  1884       }
  2018 
  1885 
  2019       // ----- Look for attribute
  1886       // ----- Look for attribute
  2020       switch ($v_key) {
  1887       switch ($v_key) {
  2021         case PCLZIP_ATT_FILE_NAME :
  1888         case PCLZIP_ATT_FILE_NAME :
  2022           if (!is_string($v_value)) {
  1889           if (!is_string($v_value)) {
  2023             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1890             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  2024             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2025             return PclZip::errorCode();
  1891             return PclZip::errorCode();
  2026           }
  1892           }
  2027 
  1893 
  2028           $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
  1894           $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
  2029           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
  1895 
  2030           
       
  2031           if ($p_filedescr['filename'] == '') {
  1896           if ($p_filedescr['filename'] == '') {
  2032             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1897             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
  2033             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2034             return PclZip::errorCode();
  1898             return PclZip::errorCode();
  2035           }
  1899           }
  2036 
  1900 
  2037         break;
  1901         break;
  2038 
  1902 
  2039         case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
  1903         case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
  2040           if (!is_string($v_value)) {
  1904           if (!is_string($v_value)) {
  2041             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1905             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  2042             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2043             return PclZip::errorCode();
  1906             return PclZip::errorCode();
  2044           }
  1907           }
  2045 
  1908 
  2046           $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
  1909           $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
  2047           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
       
  2048 
  1910 
  2049           if ($p_filedescr['new_short_name'] == '') {
  1911           if ($p_filedescr['new_short_name'] == '') {
  2050             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1912             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
  2051             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2052             return PclZip::errorCode();
  1913             return PclZip::errorCode();
  2053           }
  1914           }
  2054         break;
  1915         break;
  2055 
  1916 
  2056         case PCLZIP_ATT_FILE_NEW_FULL_NAME :
  1917         case PCLZIP_ATT_FILE_NEW_FULL_NAME :
  2057           if (!is_string($v_value)) {
  1918           if (!is_string($v_value)) {
  2058             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1919             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  2059             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2060             return PclZip::errorCode();
  1920             return PclZip::errorCode();
  2061           }
  1921           }
  2062 
  1922 
  2063           $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
  1923           $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
  2064           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
       
  2065 
  1924 
  2066           if ($p_filedescr['new_full_name'] == '') {
  1925           if ($p_filedescr['new_full_name'] == '') {
  2067             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1926             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
  2068             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2069             return PclZip::errorCode();
  1927             return PclZip::errorCode();
  2070           }
  1928           }
  2071         break;
  1929         break;
  2072 
  1930 
  2073         // ----- Look for options that takes a string
  1931         // ----- Look for options that takes a string
  2074         case PCLZIP_ATT_FILE_COMMENT :
  1932         case PCLZIP_ATT_FILE_COMMENT :
  2075           if (!is_string($v_value)) {
  1933           if (!is_string($v_value)) {
  2076             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1934             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  2077             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2078             return PclZip::errorCode();
  1935             return PclZip::errorCode();
  2079           }
  1936           }
  2080 
  1937 
  2081           $p_filedescr['comment'] = $v_value;
  1938           $p_filedescr['comment'] = $v_value;
  2082           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
       
  2083         break;
  1939         break;
  2084 
  1940 
  2085         case PCLZIP_ATT_FILE_MTIME :
  1941         case PCLZIP_ATT_FILE_MTIME :
  2086           if (!is_integer($v_value)) {
  1942           if (!is_integer($v_value)) {
  2087             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1943             PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
  2088             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2089             return PclZip::errorCode();
  1944             return PclZip::errorCode();
  2090           }
  1945           }
  2091 
  1946 
  2092           $p_filedescr['mtime'] = $v_value;
  1947           $p_filedescr['mtime'] = $v_value;
  2093           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
       
  2094         break;
  1948         break;
  2095 
  1949 
  2096         case PCLZIP_ATT_FILE_CONTENT :
  1950         case PCLZIP_ATT_FILE_CONTENT :
  2097           $p_filedescr['content'] = $v_value;
  1951           $p_filedescr['content'] = $v_value;
  2098           ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
       
  2099         break;
  1952         break;
  2100 
  1953 
  2101         default :
  1954         default :
  2102           // ----- Error log
  1955           // ----- Error log
  2103           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  1956           PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  2104 		                           "Unknown parameter '".$v_key."'");
  1957 		                           "Unknown parameter '".$v_key."'");
  2105 
  1958 
  2106           // ----- Return
  1959           // ----- Return
  2107           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2108           return PclZip::errorCode();
  1960           return PclZip::errorCode();
  2109       }
  1961       }
  2110 
  1962 
  2111       // ----- Look for mandatory options
  1963       // ----- Look for mandatory options
  2112       if ($v_requested_options !== false) {
  1964       if ($v_requested_options !== false) {
  2113         for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  1965         for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  2114           // ----- Look for mandatory option
  1966           // ----- Look for mandatory option
  2115           if ($v_requested_options[$key] == 'mandatory') {
  1967           if ($v_requested_options[$key] == 'mandatory') {
  2116             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
       
  2117             // ----- Look if present
  1968             // ----- Look if present
  2118             if (!isset($p_file_list[$key])) {
  1969             if (!isset($p_file_list[$key])) {
  2119               PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  1970               PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  2120               //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2121               return PclZip::errorCode();
  1971               return PclZip::errorCode();
  2122             }
  1972             }
  2123           }
  1973           }
  2124         }
  1974         }
  2125       }
  1975       }
  2126     
  1976 
  2127     // end foreach
  1977     // end foreach
  2128     }
  1978     }
  2129     
  1979 
  2130     // ----- Return
  1980     // ----- Return
  2131     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2132     return $v_result;
  1981     return $v_result;
  2133   }
  1982   }
  2134   // --------------------------------------------------------------------------------
  1983   // --------------------------------------------------------------------------------
  2135 
  1984 
  2136   // --------------------------------------------------------------------------------
  1985   // --------------------------------------------------------------------------------
  2138   // Description :
  1987   // Description :
  2139   //   This method look for each item of the list to see if its a file, a folder
  1988   //   This method look for each item of the list to see if its a file, a folder
  2140   //   or a string to be added as file. For any other type of files (link, other)
  1989   //   or a string to be added as file. For any other type of files (link, other)
  2141   //   just ignore the item.
  1990   //   just ignore the item.
  2142   //   Then prepare the information that will be stored for that file.
  1991   //   Then prepare the information that will be stored for that file.
  2143   //   When its a folder, expand the folder with all the files that are in that 
  1992   //   When its a folder, expand the folder with all the files that are in that
  2144   //   folder (recursively).
  1993   //   folder (recursively).
  2145   // Parameters :
  1994   // Parameters :
  2146   // Return Values :
  1995   // Return Values :
  2147   //   1 on success.
  1996   //   1 on success.
  2148   //   0 on failure.
  1997   //   0 on failure.
  2149   // --------------------------------------------------------------------------------
  1998   // --------------------------------------------------------------------------------
  2150   function privFileDescrExpand(&$p_filedescr_list, &$p_options)
  1999   function privFileDescrExpand(&$p_filedescr_list, &$p_options)
  2151   {
  2000   {
  2152     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrExpand", "");
       
  2153     $v_result=1;
  2001     $v_result=1;
  2154     
  2002 
  2155     // ----- Create a result list
  2003     // ----- Create a result list
  2156     $v_result_list = array();
  2004     $v_result_list = array();
  2157     
  2005 
  2158     // ----- Look each entry
  2006     // ----- Look each entry
  2159     for ($i=0; $i<sizeof($p_filedescr_list); $i++) {
  2007     for ($i=0; $i<sizeof($p_filedescr_list); $i++) {
  2160       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Looking for file ".$i.".");
  2008 
  2161       
       
  2162       // ----- Get filedescr
  2009       // ----- Get filedescr
  2163       $v_descr = $p_filedescr_list[$i];
  2010       $v_descr = $p_filedescr_list[$i];
  2164       
  2011 
  2165       // ----- Reduce the filename
  2012       // ----- Reduce the filename
  2166       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filedescr before reduction :'".$v_descr['filename']."'");
       
  2167       $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
  2013       $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
  2168       $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
  2014       $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
  2169       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filedescr after reduction :'".$v_descr['filename']."'");
  2015 
  2170       
       
  2171       // ----- Look for real file or folder
  2016       // ----- Look for real file or folder
  2172       if (file_exists($v_descr['filename'])) {
  2017       if (file_exists($v_descr['filename'])) {
  2173         if (@is_file($v_descr['filename'])) {
  2018         if (@is_file($v_descr['filename'])) {
  2174           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a file");
       
  2175           $v_descr['type'] = 'file';
  2019           $v_descr['type'] = 'file';
  2176         }
  2020         }
  2177         else if (@is_dir($v_descr['filename'])) {
  2021         else if (@is_dir($v_descr['filename'])) {
  2178           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a folder");
       
  2179           $v_descr['type'] = 'folder';
  2022           $v_descr['type'] = 'folder';
  2180         }
  2023         }
  2181         else if (@is_link($v_descr['filename'])) {
  2024         else if (@is_link($v_descr['filename'])) {
  2182           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Unsupported file type : link");
       
  2183           // skip
  2025           // skip
  2184           continue;
  2026           continue;
  2185         }
  2027         }
  2186         else {
  2028         else {
  2187           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Unsupported file type : unknown type");
       
  2188           // skip
  2029           // skip
  2189           continue;
  2030           continue;
  2190         }
  2031         }
  2191       }
  2032       }
  2192       
  2033 
  2193       // ----- Look for string added as file
  2034       // ----- Look for string added as file
  2194       else if (isset($v_descr['content'])) {
  2035       else if (isset($v_descr['content'])) {
  2195         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a string added as a file");
       
  2196         $v_descr['type'] = 'virtual_file';
  2036         $v_descr['type'] = 'virtual_file';
  2197       }
  2037       }
  2198       
  2038 
  2199       // ----- Missing file
  2039       // ----- Missing file
  2200       else {
  2040       else {
  2201         // ----- Error log
  2041         // ----- Error log
  2202         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_descr['filename']."' does not exist");
       
  2203         PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$v_descr['filename']."' does not exist");
  2042         PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$v_descr['filename']."' does not exist");
  2204 
  2043 
  2205         // ----- Return
  2044         // ----- Return
  2206         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2207         return PclZip::errorCode();
  2045         return PclZip::errorCode();
  2208       }
  2046       }
  2209       
  2047 
  2210       // ----- Calculate the stored filename
  2048       // ----- Calculate the stored filename
  2211       $this->privCalculateStoredFilename($v_descr, $p_options);
  2049       $this->privCalculateStoredFilename($v_descr, $p_options);
  2212       
  2050 
  2213       // ----- Add the descriptor in result list
  2051       // ----- Add the descriptor in result list
  2214       $v_result_list[sizeof($v_result_list)] = $v_descr;
  2052       $v_result_list[sizeof($v_result_list)] = $v_descr;
  2215       
  2053 
  2216       // ----- Look for folder
  2054       // ----- Look for folder
  2217       if ($v_descr['type'] == 'folder') {
  2055       if ($v_descr['type'] == 'folder') {
  2218         // ----- List of items in folder
  2056         // ----- List of items in folder
  2219         $v_dirlist_descr = array();
  2057         $v_dirlist_descr = array();
  2220         $v_dirlist_nb = 0;
  2058         $v_dirlist_nb = 0;
  2221         if ($v_folder_handler = @opendir($v_descr['filename'])) {
  2059         if ($v_folder_handler = @opendir($v_descr['filename'])) {
  2222           while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
  2060           while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
  2223             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for '".$v_item_handler."' in the directory");
       
  2224 
  2061 
  2225             // ----- Skip '.' and '..'
  2062             // ----- Skip '.' and '..'
  2226             if (($v_item_handler == '.') || ($v_item_handler == '..')) {
  2063             if (($v_item_handler == '.') || ($v_item_handler == '..')) {
  2227                 continue;
  2064                 continue;
  2228             }
  2065             }
  2229             
  2066 
  2230             // ----- Compose the full filename
  2067             // ----- Compose the full filename
  2231             $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
  2068             $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
  2232             
  2069 
  2233             // ----- Look for different stored filename
  2070             // ----- Look for different stored filename
  2234             // Because the name of the folder was changed, the name of the
  2071             // Because the name of the folder was changed, the name of the
  2235             // files/sub-folders also change
  2072             // files/sub-folders also change
  2236             if ($v_descr['stored_filename'] != $v_descr['filename']) {
  2073             if (($v_descr['stored_filename'] != $v_descr['filename'])
       
  2074                  && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
  2237               if ($v_descr['stored_filename'] != '') {
  2075               if ($v_descr['stored_filename'] != '') {
  2238                 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
  2076                 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
  2239               }
  2077               }
  2240               else {
  2078               else {
  2241                 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
  2079                 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
  2242               }
  2080               }
  2243             }
  2081             }
  2244       
  2082 
  2245             $v_dirlist_nb++;
  2083             $v_dirlist_nb++;
  2246           }
  2084           }
  2247           
  2085 
  2248           @closedir($v_folder_handler);
  2086           @closedir($v_folder_handler);
  2249         }
  2087         }
  2250         else {
  2088         else {
  2251           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to open dir '".$v_descr['filename']."' in read mode. Skipped.");
       
  2252           // TBC : unable to open folder in read mode
  2089           // TBC : unable to open folder in read mode
  2253         }
  2090         }
  2254         
  2091 
  2255         // ----- Expand each element of the list
  2092         // ----- Expand each element of the list
  2256         if ($v_dirlist_nb != 0) {
  2093         if ($v_dirlist_nb != 0) {
  2257           // ----- Expand
  2094           // ----- Expand
  2258           if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
  2095           if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
  2259             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2260             return $v_result;
  2096             return $v_result;
  2261           }
  2097           }
  2262           
  2098 
  2263           // ----- Concat the resulting list
  2099           // ----- Concat the resulting list
  2264           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Merging result list (size '".sizeof($v_result_list)."') with dirlist (size '".sizeof($v_dirlist_descr)."')");
       
  2265           $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
  2100           $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
  2266           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "merged result list is size '".sizeof($v_result_list)."'");
       
  2267         }
  2101         }
  2268         else {
  2102         else {
  2269           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Nothing in this folder to expand.");
  2103         }
  2270         }
  2104 
  2271           
       
  2272         // ----- Free local array
  2105         // ----- Free local array
  2273         unset($v_dirlist_descr);
  2106         unset($v_dirlist_descr);
  2274       }
  2107       }
  2275     }
  2108     }
  2276     
  2109 
  2277     // ----- Get the result list
  2110     // ----- Get the result list
  2278     $p_filedescr_list = $v_result_list;
  2111     $p_filedescr_list = $v_result_list;
  2279 
  2112 
  2280     // ----- Return
  2113     // ----- Return
  2281     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2282     return $v_result;
  2114     return $v_result;
  2283   }
  2115   }
  2284   // --------------------------------------------------------------------------------
  2116   // --------------------------------------------------------------------------------
  2285 
  2117 
  2286   // --------------------------------------------------------------------------------
  2118   // --------------------------------------------------------------------------------
  2289   // Parameters :
  2121   // Parameters :
  2290   // Return Values :
  2122   // Return Values :
  2291   // --------------------------------------------------------------------------------
  2123   // --------------------------------------------------------------------------------
  2292   function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
  2124   function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
  2293   {
  2125   {
  2294     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCreate", "list");
       
  2295     $v_result=1;
  2126     $v_result=1;
  2296     $v_list_detail = array();
  2127     $v_list_detail = array();
  2297     
  2128 
  2298     // ----- Magic quotes trick
  2129     // ----- Magic quotes trick
  2299     $this->privDisableMagicQuotes();
  2130     $this->privDisableMagicQuotes();
  2300 
  2131 
  2301     // ----- Open the file in write mode
  2132     // ----- Open the file in write mode
  2302     if (($v_result = $this->privOpenFd('wb')) != 1)
  2133     if (($v_result = $this->privOpenFd('wb')) != 1)
  2303     {
  2134     {
  2304       // ----- Return
  2135       // ----- Return
  2305       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2306       return $v_result;
  2136       return $v_result;
  2307     }
  2137     }
  2308 
  2138 
  2309     // ----- Add the list of files
  2139     // ----- Add the list of files
  2310     $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
  2140     $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
  2314 
  2144 
  2315     // ----- Magic quotes trick
  2145     // ----- Magic quotes trick
  2316     $this->privSwapBackMagicQuotes();
  2146     $this->privSwapBackMagicQuotes();
  2317 
  2147 
  2318     // ----- Return
  2148     // ----- Return
  2319     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2320     return $v_result;
  2149     return $v_result;
  2321   }
  2150   }
  2322   // --------------------------------------------------------------------------------
  2151   // --------------------------------------------------------------------------------
  2323 
  2152 
  2324   // --------------------------------------------------------------------------------
  2153   // --------------------------------------------------------------------------------
  2327   // Parameters :
  2156   // Parameters :
  2328   // Return Values :
  2157   // Return Values :
  2329   // --------------------------------------------------------------------------------
  2158   // --------------------------------------------------------------------------------
  2330   function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
  2159   function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
  2331   {
  2160   {
  2332     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAdd", "list");
       
  2333     $v_result=1;
  2161     $v_result=1;
  2334     $v_list_detail = array();
  2162     $v_list_detail = array();
  2335 
  2163 
  2336     // ----- Look if the archive exists or is empty
  2164     // ----- Look if the archive exists or is empty
  2337     if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
  2165     if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
  2338     {
  2166     {
  2339       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, or is empty, create it.");
       
  2340 
  2167 
  2341       // ----- Do a create
  2168       // ----- Do a create
  2342       $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
  2169       $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
  2343 
  2170 
  2344       // ----- Return
  2171       // ----- Return
  2345       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2346       return $v_result;
  2172       return $v_result;
  2347     }
  2173     }
  2348     // ----- Magic quotes trick
  2174     // ----- Magic quotes trick
  2349     $this->privDisableMagicQuotes();
  2175     $this->privDisableMagicQuotes();
  2350 
  2176 
  2351     // ----- Open the zip file
  2177     // ----- Open the zip file
  2352     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  2353     if (($v_result=$this->privOpenFd('rb')) != 1)
  2178     if (($v_result=$this->privOpenFd('rb')) != 1)
  2354     {
  2179     {
  2355       // ----- Magic quotes trick
  2180       // ----- Magic quotes trick
  2356       $this->privSwapBackMagicQuotes();
  2181       $this->privSwapBackMagicQuotes();
  2357 
  2182 
  2358       // ----- Return
  2183       // ----- Return
  2359       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2360       return $v_result;
  2184       return $v_result;
  2361     }
  2185     }
  2362 
  2186 
  2363     // ----- Read the central directory informations
  2187     // ----- Read the central directory informations
  2364     $v_central_dir = array();
  2188     $v_central_dir = array();
  2365     if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  2189     if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  2366     {
  2190     {
  2367       $this->privCloseFd();
  2191       $this->privCloseFd();
  2368       $this->privSwapBackMagicQuotes();
  2192       $this->privSwapBackMagicQuotes();
  2369       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2370       return $v_result;
  2193       return $v_result;
  2371     }
  2194     }
  2372 
  2195 
  2373     // ----- Go to beginning of File
  2196     // ----- Go to beginning of File
  2374     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
       
  2375     @rewind($this->zip_fd);
  2197     @rewind($this->zip_fd);
  2376     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
       
  2377 
  2198 
  2378     // ----- Creates a temporay file
  2199     // ----- Creates a temporay file
  2379     $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  2200     $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  2380 
  2201 
  2381     // ----- Open the temporary file in write mode
  2202     // ----- Open the temporary file in write mode
  2382     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  2383     if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  2203     if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  2384     {
  2204     {
  2385       $this->privCloseFd();
  2205       $this->privCloseFd();
  2386       $this->privSwapBackMagicQuotes();
  2206       $this->privSwapBackMagicQuotes();
  2387 
  2207 
  2388       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  2208       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  2389 
  2209 
  2390       // ----- Return
  2210       // ----- Return
  2391       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2392       return PclZip::errorCode();
  2211       return PclZip::errorCode();
  2393     }
  2212     }
  2394 
  2213 
  2395     // ----- Copy the files from the archive to the temporary file
  2214     // ----- Copy the files from the archive to the temporary file
  2396     // TBC : Here I should better append the file and go back to erase the central dir
  2215     // TBC : Here I should better append the file and go back to erase the central dir
  2397     $v_size = $v_central_dir['offset'];
  2216     $v_size = $v_central_dir['offset'];
  2398     while ($v_size != 0)
  2217     while ($v_size != 0)
  2399     {
  2218     {
  2400       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2219       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2401       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  2402       $v_buffer = fread($this->zip_fd, $v_read_size);
  2220       $v_buffer = fread($this->zip_fd, $v_read_size);
  2403       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2221       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2404       $v_size -= $v_read_size;
  2222       $v_size -= $v_read_size;
  2405     }
  2223     }
  2406 
  2224 
  2419       $this->privCloseFd();
  2237       $this->privCloseFd();
  2420       @unlink($v_zip_temp_name);
  2238       @unlink($v_zip_temp_name);
  2421       $this->privSwapBackMagicQuotes();
  2239       $this->privSwapBackMagicQuotes();
  2422 
  2240 
  2423       // ----- Return
  2241       // ----- Return
  2424       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2425       return $v_result;
  2242       return $v_result;
  2426     }
  2243     }
  2427 
  2244 
  2428     // ----- Store the offset of the central dir
  2245     // ----- Store the offset of the central dir
  2429     $v_offset = @ftell($this->zip_fd);
  2246     $v_offset = @ftell($this->zip_fd);
  2430     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
       
  2431 
  2247 
  2432     // ----- Copy the block of file headers from the old archive
  2248     // ----- Copy the block of file headers from the old archive
  2433     $v_size = $v_central_dir['size'];
  2249     $v_size = $v_central_dir['size'];
  2434     while ($v_size != 0)
  2250     while ($v_size != 0)
  2435     {
  2251     {
  2436       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2252       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2437       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  2438       $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  2253       $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  2439       @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  2254       @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  2440       $v_size -= $v_read_size;
  2255       $v_size -= $v_read_size;
  2441     }
  2256     }
  2442 
  2257 
  2450           $this->privCloseFd();
  2265           $this->privCloseFd();
  2451           @unlink($v_zip_temp_name);
  2266           @unlink($v_zip_temp_name);
  2452           $this->privSwapBackMagicQuotes();
  2267           $this->privSwapBackMagicQuotes();
  2453 
  2268 
  2454           // ----- Return
  2269           // ----- Return
  2455           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2456           return $v_result;
  2270           return $v_result;
  2457         }
  2271         }
  2458         $v_count++;
  2272         $v_count++;
  2459       }
  2273       }
  2460 
  2274 
  2483       // ----- Reset the file list
  2297       // ----- Reset the file list
  2484       unset($v_header_list);
  2298       unset($v_header_list);
  2485       $this->privSwapBackMagicQuotes();
  2299       $this->privSwapBackMagicQuotes();
  2486 
  2300 
  2487       // ----- Return
  2301       // ----- Return
  2488       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2489       return $v_result;
  2302       return $v_result;
  2490     }
  2303     }
  2491 
  2304 
  2492     // ----- Swap back the file descriptor
  2305     // ----- Swap back the file descriptor
  2493     $v_swap = $this->zip_fd;
  2306     $v_swap = $this->zip_fd;
  2511     // TBC : I should test the result ...
  2324     // TBC : I should test the result ...
  2512     //@rename($v_zip_temp_name, $this->zipname);
  2325     //@rename($v_zip_temp_name, $this->zipname);
  2513     PclZipUtilRename($v_zip_temp_name, $this->zipname);
  2326     PclZipUtilRename($v_zip_temp_name, $this->zipname);
  2514 
  2327 
  2515     // ----- Return
  2328     // ----- Return
  2516     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2517     return $v_result;
  2329     return $v_result;
  2518   }
  2330   }
  2519   // --------------------------------------------------------------------------------
  2331   // --------------------------------------------------------------------------------
  2520 
  2332 
  2521   // --------------------------------------------------------------------------------
  2333   // --------------------------------------------------------------------------------
  2523   // Description :
  2335   // Description :
  2524   // Parameters :
  2336   // Parameters :
  2525   // --------------------------------------------------------------------------------
  2337   // --------------------------------------------------------------------------------
  2526   function privOpenFd($p_mode)
  2338   function privOpenFd($p_mode)
  2527   {
  2339   {
  2528     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privOpenFd", 'mode='.$p_mode);
       
  2529     $v_result=1;
  2340     $v_result=1;
  2530 
  2341 
  2531     // ----- Look if already open
  2342     // ----- Look if already open
  2532     if ($this->zip_fd != 0)
  2343     if ($this->zip_fd != 0)
  2533     {
  2344     {
  2534       // ----- Error log
  2345       // ----- Error log
  2535       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
  2346       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
  2536 
  2347 
  2537       // ----- Return
  2348       // ----- Return
  2538       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2539       return PclZip::errorCode();
  2349       return PclZip::errorCode();
  2540     }
  2350     }
  2541 
  2351 
  2542     // ----- Open the zip file
  2352     // ----- Open the zip file
  2543     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Open file in '.$p_mode.' mode');
       
  2544     if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
  2353     if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
  2545     {
  2354     {
  2546       // ----- Error log
  2355       // ----- Error log
  2547       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
  2356       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
  2548 
  2357 
  2549       // ----- Return
  2358       // ----- Return
  2550       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2551       return PclZip::errorCode();
  2359       return PclZip::errorCode();
  2552     }
  2360     }
  2553 
  2361 
  2554     // ----- Return
  2362     // ----- Return
  2555     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2556     return $v_result;
  2363     return $v_result;
  2557   }
  2364   }
  2558   // --------------------------------------------------------------------------------
  2365   // --------------------------------------------------------------------------------
  2559 
  2366 
  2560   // --------------------------------------------------------------------------------
  2367   // --------------------------------------------------------------------------------
  2562   // Description :
  2369   // Description :
  2563   // Parameters :
  2370   // Parameters :
  2564   // --------------------------------------------------------------------------------
  2371   // --------------------------------------------------------------------------------
  2565   function privCloseFd()
  2372   function privCloseFd()
  2566   {
  2373   {
  2567     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCloseFd", "");
       
  2568     $v_result=1;
  2374     $v_result=1;
  2569 
  2375 
  2570     if ($this->zip_fd != 0)
  2376     if ($this->zip_fd != 0)
  2571       @fclose($this->zip_fd);
  2377       @fclose($this->zip_fd);
  2572     $this->zip_fd = 0;
  2378     $this->zip_fd = 0;
  2573 
  2379 
  2574     // ----- Return
  2380     // ----- Return
  2575     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2576     return $v_result;
  2381     return $v_result;
  2577   }
  2382   }
  2578   // --------------------------------------------------------------------------------
  2383   // --------------------------------------------------------------------------------
  2579 
  2384 
  2580   // --------------------------------------------------------------------------------
  2385   // --------------------------------------------------------------------------------
  2591   // Return Values :
  2396   // Return Values :
  2592   // --------------------------------------------------------------------------------
  2397   // --------------------------------------------------------------------------------
  2593 //  function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  2398 //  function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  2594   function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
  2399   function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
  2595   {
  2400   {
  2596     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddList", "list");
       
  2597     $v_result=1;
  2401     $v_result=1;
  2598 
  2402 
  2599     // ----- Add the files
  2403     // ----- Add the files
  2600     $v_header_list = array();
  2404     $v_header_list = array();
  2601     if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
  2405     if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
  2602     {
  2406     {
  2603       // ----- Return
  2407       // ----- Return
  2604       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2605       return $v_result;
  2408       return $v_result;
  2606     }
  2409     }
  2607 
  2410 
  2608     // ----- Store the offset of the central dir
  2411     // ----- Store the offset of the central dir
  2609     $v_offset = @ftell($this->zip_fd);
  2412     $v_offset = @ftell($this->zip_fd);
  2613     {
  2416     {
  2614       // ----- Create the file header
  2417       // ----- Create the file header
  2615       if ($v_header_list[$i]['status'] == 'ok') {
  2418       if ($v_header_list[$i]['status'] == 'ok') {
  2616         if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  2419         if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  2617           // ----- Return
  2420           // ----- Return
  2618           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2619           return $v_result;
  2421           return $v_result;
  2620         }
  2422         }
  2621         $v_count++;
  2423         $v_count++;
  2622       }
  2424       }
  2623 
  2425 
  2639     {
  2441     {
  2640       // ----- Reset the file list
  2442       // ----- Reset the file list
  2641       unset($v_header_list);
  2443       unset($v_header_list);
  2642 
  2444 
  2643       // ----- Return
  2445       // ----- Return
  2644       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2645       return $v_result;
  2446       return $v_result;
  2646     }
  2447     }
  2647 
  2448 
  2648     // ----- Return
  2449     // ----- Return
  2649     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2650     return $v_result;
  2450     return $v_result;
  2651   }
  2451   }
  2652   // --------------------------------------------------------------------------------
  2452   // --------------------------------------------------------------------------------
  2653 
  2453 
  2654   // --------------------------------------------------------------------------------
  2454   // --------------------------------------------------------------------------------
  2655   // Function : privAddFileList()
  2455   // Function : privAddFileList()
  2656   // Description :
  2456   // Description :
  2657   // Parameters :
  2457   // Parameters :
  2658   //   $p_filedescr_list : An array containing the file description 
  2458   //   $p_filedescr_list : An array containing the file description
  2659   //                      or directory names to add in the zip
  2459   //                      or directory names to add in the zip
  2660   //   $p_result_list : list of added files with their properties (specially the status field)
  2460   //   $p_result_list : list of added files with their properties (specially the status field)
  2661   // Return Values :
  2461   // Return Values :
  2662   // --------------------------------------------------------------------------------
  2462   // --------------------------------------------------------------------------------
  2663   function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
  2463   function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
  2664   {
  2464   {
  2665     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFileList", "filedescr_list");
       
  2666     $v_result=1;
  2465     $v_result=1;
  2667     $v_header = array();
  2466     $v_header = array();
  2668 
  2467 
  2669     // ----- Recuperate the current number of elt in list
  2468     // ----- Recuperate the current number of elt in list
  2670     $v_nb = sizeof($p_result_list);
  2469     $v_nb = sizeof($p_result_list);
  2671     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Before add, list have ".$v_nb." elements");
       
  2672 
  2470 
  2673     // ----- Loop on the files
  2471     // ----- Loop on the files
  2674     for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++) {
  2472     for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++) {
  2675       // ----- Format the filename
  2473       // ----- Format the filename
  2676       $p_filedescr_list[$j]['filename']
  2474       $p_filedescr_list[$j]['filename']
  2677       = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
  2475       = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
  2678       
  2476 
  2679       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for file '".$p_filedescr_list[$j]['filename']."'");
       
  2680 
  2477 
  2681       // ----- Skip empty file names
  2478       // ----- Skip empty file names
  2682       // TBC : Can this be possible ? not checked in DescrParseAtt ?
  2479       // TBC : Can this be possible ? not checked in DescrParseAtt ?
  2683       if ($p_filedescr_list[$j]['filename'] == "") {
  2480       if ($p_filedescr_list[$j]['filename'] == "") {
  2684         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Skip empty filename");
       
  2685         continue;
  2481         continue;
  2686       }
  2482       }
  2687 
  2483 
  2688       // ----- Check the filename
  2484       // ----- Check the filename
  2689       if (   ($p_filedescr_list[$j]['type'] != 'virtual_file')
  2485       if (   ($p_filedescr_list[$j]['type'] != 'virtual_file')
  2690           && (!file_exists($p_filedescr_list[$j]['filename']))) {
  2486           && (!file_exists($p_filedescr_list[$j]['filename']))) {
  2691         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
       
  2692         PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
  2487         PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
  2693         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2694         return PclZip::errorCode();
  2488         return PclZip::errorCode();
  2695       }
  2489       }
  2696 
  2490 
  2697       // ----- Look if it is a file or a dir with no all path remove option
  2491       // ----- Look if it is a file or a dir with no all path remove option
  2698       // or a dir with all its path removed
  2492       // or a dir with all its path removed
  2707 
  2501 
  2708         // ----- Add the file
  2502         // ----- Add the file
  2709         $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
  2503         $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
  2710                                        $p_options);
  2504                                        $p_options);
  2711         if ($v_result != 1) {
  2505         if ($v_result != 1) {
  2712           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2713           return $v_result;
  2506           return $v_result;
  2714         }
  2507         }
  2715 
  2508 
  2716         // ----- Store the file infos
  2509         // ----- Store the file infos
  2717         $p_result_list[$v_nb++] = $v_header;
  2510         $p_result_list[$v_nb++] = $v_header;
  2718       }
  2511       }
  2719     }
  2512     }
  2720     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "After add, list have ".$v_nb." elements");
       
  2721 
  2513 
  2722     // ----- Return
  2514     // ----- Return
  2723     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2724     return $v_result;
  2515     return $v_result;
  2725   }
  2516   }
  2726   // --------------------------------------------------------------------------------
  2517   // --------------------------------------------------------------------------------
  2727 
  2518 
  2728   // --------------------------------------------------------------------------------
  2519   // --------------------------------------------------------------------------------
  2731   // Parameters :
  2522   // Parameters :
  2732   // Return Values :
  2523   // Return Values :
  2733   // --------------------------------------------------------------------------------
  2524   // --------------------------------------------------------------------------------
  2734   function privAddFile($p_filedescr, &$p_header, &$p_options)
  2525   function privAddFile($p_filedescr, &$p_header, &$p_options)
  2735   {
  2526   {
  2736     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFile", "filename='".$p_filedescr['filename']."'");
       
  2737     $v_result=1;
  2527     $v_result=1;
  2738     
  2528 
  2739     // ----- Working variable
  2529     // ----- Working variable
  2740     $p_filename = $p_filedescr['filename'];
  2530     $p_filename = $p_filedescr['filename'];
  2741 
  2531 
  2742     // TBC : Already done in the fileAtt check ... ?
  2532     // TBC : Already done in the fileAtt check ... ?
  2743     if ($p_filename == "") {
  2533     if ($p_filename == "") {
  2744       // ----- Error log
  2534       // ----- Error log
  2745       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  2535       PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  2746 
  2536 
  2747       // ----- Return
  2537       // ----- Return
  2748       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2749       return PclZip::errorCode();
  2538       return PclZip::errorCode();
  2750     }
  2539     }
  2751   
  2540 
  2752     // ----- Look for a stored different filename 
  2541     // ----- Look for a stored different filename
  2753     /* TBC : Removed
  2542     /* TBC : Removed
  2754     if (isset($p_filedescr['stored_filename'])) {
  2543     if (isset($p_filedescr['stored_filename'])) {
  2755       $v_stored_filename = $p_filedescr['stored_filename'];
  2544       $v_stored_filename = $p_filedescr['stored_filename'];
  2756       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is NOT the same "'.$v_stored_filename.'"');
       
  2757     }
  2545     }
  2758     else {
  2546     else {
  2759       $v_stored_filename = $p_filedescr['stored_filename'];
  2547       $v_stored_filename = $p_filedescr['stored_filename'];
  2760       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is the same');
       
  2761     }
  2548     }
  2762     */
  2549     */
  2763 
  2550 
  2764     // ----- Set the file properties
  2551     // ----- Set the file properties
  2765     clearstatcache();
  2552     clearstatcache();
  2784     // ----- Look for regular file
  2571     // ----- Look for regular file
  2785     if ($p_filedescr['type']=='file') {
  2572     if ($p_filedescr['type']=='file') {
  2786       $p_header['external'] = 0x00000000;
  2573       $p_header['external'] = 0x00000000;
  2787       $p_header['size'] = filesize($p_filename);
  2574       $p_header['size'] = filesize($p_filename);
  2788     }
  2575     }
  2789     
  2576 
  2790     // ----- Look for regular folder
  2577     // ----- Look for regular folder
  2791     else if ($p_filedescr['type']=='folder') {
  2578     else if ($p_filedescr['type']=='folder') {
  2792       $p_header['external'] = 0x00000010;
  2579       $p_header['external'] = 0x00000010;
  2793       $p_header['mtime'] = filemtime($p_filename);
  2580       $p_header['mtime'] = filemtime($p_filename);
  2794       $p_header['size'] = filesize($p_filename);
  2581       $p_header['size'] = filesize($p_filename);
  2795     }
  2582     }
  2796     
  2583 
  2797     // ----- Look for virtual file
  2584     // ----- Look for virtual file
  2798     else if ($p_filedescr['type'] == 'virtual_file') {
  2585     else if ($p_filedescr['type'] == 'virtual_file') {
  2799       $p_header['external'] = 0x00000000;
  2586       $p_header['external'] = 0x00000000;
  2800       $p_header['size'] = strlen($p_filedescr['content']);
  2587       $p_header['size'] = strlen($p_filedescr['content']);
  2801     }
  2588     }
  2802     
  2589 
  2803     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header external extension '".sprintf("0x%X",$p_header['external'])."'");
       
  2804 
  2590 
  2805     // ----- Look for filetime
  2591     // ----- Look for filetime
  2806     if (isset($p_filedescr['mtime'])) {
  2592     if (isset($p_filedescr['mtime'])) {
  2807       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Overload mtime value with :'".$p_filedescr['mtime']."'");
       
  2808       $p_header['mtime'] = $p_filedescr['mtime'];
  2593       $p_header['mtime'] = $p_filedescr['mtime'];
  2809     }
  2594     }
  2810     else if ($p_filedescr['type'] == 'virtual_file') {
  2595     else if ($p_filedescr['type'] == 'virtual_file') {
  2811       $p_header['mtime'] = time();
  2596       $p_header['mtime'] = time();
  2812       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Virtual file : use current time '".$p_header['mtime']."' for mtime value.");
       
  2813     }
  2597     }
  2814     else {
  2598     else {
  2815       $p_header['mtime'] = filemtime($p_filename);
  2599       $p_header['mtime'] = filemtime($p_filename);
  2816     }
  2600     }
  2817 
  2601 
  2825       $p_header['comment'] = '';
  2609       $p_header['comment'] = '';
  2826     }
  2610     }
  2827 
  2611 
  2828     // ----- Look for pre-add callback
  2612     // ----- Look for pre-add callback
  2829     if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
  2613     if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
  2830       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_ADD]."()') is defined for the extraction");
       
  2831 
  2614 
  2832       // ----- Generate a local information
  2615       // ----- Generate a local information
  2833       $v_local_header = array();
  2616       $v_local_header = array();
  2834       $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  2617       $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  2835 
  2618 
  2836       // ----- Call the callback
  2619       // ----- Call the callback
  2837       // Here I do not use call_user_func() because I need to send a reference to the
  2620       // Here I do not use call_user_func() because I need to send a reference to the
  2838       // header.
  2621       // header.
  2839       eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
  2622       $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
  2840       if ($v_result == 0) {
  2623       if ($v_result == 0) {
  2841         // ----- Change the file status
  2624         // ----- Change the file status
  2842         $p_header['status'] = "skipped";
  2625         $p_header['status'] = "skipped";
  2843         $v_result = 1;
  2626         $v_result = 1;
  2844       }
  2627       }
  2845 
  2628 
  2846       // ----- Update the informations
  2629       // ----- Update the informations
  2847       // Only some fields can be modified
  2630       // Only some fields can be modified
  2848       if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  2631       if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  2849         $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
  2632         $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
  2850         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New stored filename is '".$p_header['stored_filename']."'");
       
  2851       }
  2633       }
  2852     }
  2634     }
  2853 
  2635 
  2854     // ----- Look for empty stored filename
  2636     // ----- Look for empty stored filename
  2855     if ($p_header['stored_filename'] == "") {
  2637     if ($p_header['stored_filename'] == "") {
  2856       $p_header['status'] = "filtered";
  2638       $p_header['status'] = "filtered";
  2857     }
  2639     }
  2858     
  2640 
  2859     // ----- Check the path length
  2641     // ----- Check the path length
  2860     if (strlen($p_header['stored_filename']) > 0xFF) {
  2642     if (strlen($p_header['stored_filename']) > 0xFF) {
  2861       $p_header['status'] = 'filename_too_long';
  2643       $p_header['status'] = 'filename_too_long';
  2862     }
  2644     }
  2863 
  2645 
  2865     if ($p_header['status'] == 'ok') {
  2647     if ($p_header['status'] == 'ok') {
  2866 
  2648 
  2867       // ----- Look for a file
  2649       // ----- Look for a file
  2868       if ($p_filedescr['type'] == 'file') {
  2650       if ($p_filedescr['type'] == 'file') {
  2869         // ----- Look for using temporary file to zip
  2651         // ----- Look for using temporary file to zip
  2870         if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) 
  2652         if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  2871             && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  2653             && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  2872                 || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  2654                 || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  2873                     && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])) ) ) {
  2655                     && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])) ) ) {
  2874           $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
  2656           $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
  2875           if ($v_result < PCLZIP_ERR_NO_ERROR) {
  2657           if ($v_result < PCLZIP_ERR_NO_ERROR) {
  2876             return $v_result;
  2658             return $v_result;
  2877           }
  2659           }
  2878         }
  2660         }
  2879         
  2661 
  2880         // ----- Use "in memory" zip algo
  2662         // ----- Use "in memory" zip algo
  2881         else {
  2663         else {
  2882           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"In memory compression.");      
       
  2883           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Current memory usage : ".memory_get_usage(TRUE)." bytes");      
       
  2884           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Current memory peak : ".memory_get_peak_usage(TRUE)." bytes");      
       
  2885           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a file");
       
  2886 
  2664 
  2887         // ----- Open the source file
  2665         // ----- Open the source file
  2888         if (($v_file = @fopen($p_filename, "rb")) == 0) {
  2666         if (($v_file = @fopen($p_filename, "rb")) == 0) {
  2889           PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  2667           PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  2890           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  2891           return PclZip::errorCode();
  2668           return PclZip::errorCode();
  2892         }
  2669         }
  2893 
  2670 
  2894         // ----- Read the file content
  2671         // ----- Read the file content
  2895         $v_content = @fread($v_file, $p_header['size']);
  2672         $v_content = @fread($v_file, $p_header['size']);
  2896         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory usage after reading file : ".memory_get_usage(TRUE)." bytes");      
       
  2897         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory peak after reading file : ".memory_get_peak_usage(TRUE)." bytes");      
       
  2898 
  2673 
  2899         // ----- Close the file
  2674         // ----- Close the file
  2900         @fclose($v_file);
  2675         @fclose($v_file);
  2901 
  2676 
  2902         // ----- Calculate the CRC
  2677         // ----- Calculate the CRC
  2903         $p_header['crc'] = @crc32($v_content);
  2678         $p_header['crc'] = @crc32($v_content);
  2904         
  2679 
  2905         // ----- Look for no compression
  2680         // ----- Look for no compression
  2906         if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2681         if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2907           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be compressed");
       
  2908           // ----- Set header parameters
  2682           // ----- Set header parameters
  2909           $p_header['compressed_size'] = $p_header['size'];
  2683           $p_header['compressed_size'] = $p_header['size'];
  2910           $p_header['compression'] = 0;
  2684           $p_header['compression'] = 0;
  2911         }
  2685         }
  2912         
  2686 
  2913         // ----- Look for normal compression
  2687         // ----- Look for normal compression
  2914         else {
  2688         else {
  2915           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will be compressed");
       
  2916           // ----- Compress the content
  2689           // ----- Compress the content
  2917           $v_content = @gzdeflate($v_content);
  2690           $v_content = @gzdeflate($v_content);
  2918           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory usage after gzdeflate : ".memory_get_usage(TRUE)." bytes");      
       
  2919           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory peak after gzdeflate : ".memory_get_peak_usage(TRUE)." bytes");      
       
  2920 
  2691 
  2921           // ----- Set header parameters
  2692           // ----- Set header parameters
  2922           $p_header['compressed_size'] = strlen($v_content);
  2693           $p_header['compressed_size'] = strlen($v_content);
  2923           $p_header['compression'] = 8;
  2694           $p_header['compression'] = 8;
  2924         }
  2695         }
  2925         
  2696 
  2926         // ----- Call the header generation
  2697         // ----- Call the header generation
  2927         if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2698         if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2928           @fclose($v_file);
  2699           @fclose($v_file);
  2929           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2930           return $v_result;
  2700           return $v_result;
  2931         }
  2701         }
  2932 
  2702 
  2933         // ----- Write the compressed (or not) content
  2703         // ----- Write the compressed (or not) content
  2934         @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2704         @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2937 
  2707 
  2938       }
  2708       }
  2939 
  2709 
  2940       // ----- Look for a virtual file (a file from string)
  2710       // ----- Look for a virtual file (a file from string)
  2941       else if ($p_filedescr['type'] == 'virtual_file') {
  2711       else if ($p_filedescr['type'] == 'virtual_file') {
  2942           
  2712 
  2943         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Add by string");
       
  2944         $v_content = $p_filedescr['content'];
  2713         $v_content = $p_filedescr['content'];
  2945 
  2714 
  2946         // ----- Calculate the CRC
  2715         // ----- Calculate the CRC
  2947         $p_header['crc'] = @crc32($v_content);
  2716         $p_header['crc'] = @crc32($v_content);
  2948         
  2717 
  2949         // ----- Look for no compression
  2718         // ----- Look for no compression
  2950         if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2719         if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2951           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be compressed");
       
  2952           // ----- Set header parameters
  2720           // ----- Set header parameters
  2953           $p_header['compressed_size'] = $p_header['size'];
  2721           $p_header['compressed_size'] = $p_header['size'];
  2954           $p_header['compression'] = 0;
  2722           $p_header['compression'] = 0;
  2955         }
  2723         }
  2956         
  2724 
  2957         // ----- Look for normal compression
  2725         // ----- Look for normal compression
  2958         else {
  2726         else {
  2959           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will be compressed");
       
  2960           // ----- Compress the content
  2727           // ----- Compress the content
  2961           $v_content = @gzdeflate($v_content);
  2728           $v_content = @gzdeflate($v_content);
  2962 
  2729 
  2963           // ----- Set header parameters
  2730           // ----- Set header parameters
  2964           $p_header['compressed_size'] = strlen($v_content);
  2731           $p_header['compressed_size'] = strlen($v_content);
  2965           $p_header['compression'] = 8;
  2732           $p_header['compression'] = 8;
  2966         }
  2733         }
  2967         
  2734 
  2968         // ----- Call the header generation
  2735         // ----- Call the header generation
  2969         if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2736         if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2970           @fclose($v_file);
  2737           @fclose($v_file);
  2971           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2972           return $v_result;
  2738           return $v_result;
  2973         }
  2739         }
  2974 
  2740 
  2975         // ----- Write the compressed (or not) content
  2741         // ----- Write the compressed (or not) content
  2976         @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2742         @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2977       }
  2743       }
  2978 
  2744 
  2979       // ----- Look for a directory
  2745       // ----- Look for a directory
  2980       else if ($p_filedescr['type'] == 'folder') {
  2746       else if ($p_filedescr['type'] == 'folder') {
  2981         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a folder");
       
  2982         // ----- Look for directory last '/'
  2747         // ----- Look for directory last '/'
  2983         if (@substr($p_header['stored_filename'], -1) != '/') {
  2748         if (@substr($p_header['stored_filename'], -1) != '/') {
  2984           $p_header['stored_filename'] .= '/';
  2749           $p_header['stored_filename'] .= '/';
  2985         }
  2750         }
  2986 
  2751 
  2990         $p_header['external'] = 0x00000010;   // Value for a folder : to be checked
  2755         $p_header['external'] = 0x00000010;   // Value for a folder : to be checked
  2991 
  2756 
  2992         // ----- Call the header generation
  2757         // ----- Call the header generation
  2993         if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
  2758         if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
  2994         {
  2759         {
  2995           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  2996           return $v_result;
  2760           return $v_result;
  2997         }
  2761         }
  2998       }
  2762       }
  2999     }
  2763     }
  3000 
  2764 
  3001     // ----- Look for post-add callback
  2765     // ----- Look for post-add callback
  3002     if (isset($p_options[PCLZIP_CB_POST_ADD])) {
  2766     if (isset($p_options[PCLZIP_CB_POST_ADD])) {
  3003       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_ADD]."()') is defined for the extraction");
       
  3004 
  2767 
  3005       // ----- Generate a local information
  2768       // ----- Generate a local information
  3006       $v_local_header = array();
  2769       $v_local_header = array();
  3007       $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  2770       $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  3008 
  2771 
  3009       // ----- Call the callback
  2772       // ----- Call the callback
  3010       // Here I do not use call_user_func() because I need to send a reference to the
  2773       // Here I do not use call_user_func() because I need to send a reference to the
  3011       // header.
  2774       // header.
  3012       eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
  2775       $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
  3013       if ($v_result == 0) {
  2776       if ($v_result == 0) {
  3014         // ----- Ignored
  2777         // ----- Ignored
  3015         $v_result = 1;
  2778         $v_result = 1;
  3016       }
  2779       }
  3017 
  2780 
  3018       // ----- Update the informations
  2781       // ----- Update the informations
  3019       // Nothing can be modified
  2782       // Nothing can be modified
  3020     }
  2783     }
  3021 
  2784 
  3022     // ----- Return
  2785     // ----- Return
  3023     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3024     return $v_result;
  2786     return $v_result;
  3025   }
  2787   }
  3026   // --------------------------------------------------------------------------------
  2788   // --------------------------------------------------------------------------------
  3027 
  2789 
  3028   // --------------------------------------------------------------------------------
  2790   // --------------------------------------------------------------------------------
  3031   // Parameters :
  2793   // Parameters :
  3032   // Return Values :
  2794   // Return Values :
  3033   // --------------------------------------------------------------------------------
  2795   // --------------------------------------------------------------------------------
  3034   function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
  2796   function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
  3035   {
  2797   {
  3036     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFileUsingTempFile", "filename='".$p_filedescr['filename']."'");
       
  3037     $v_result=PCLZIP_ERR_NO_ERROR;
  2798     $v_result=PCLZIP_ERR_NO_ERROR;
  3038     
  2799 
  3039     // ----- Working variable
  2800     // ----- Working variable
  3040     $p_filename = $p_filedescr['filename'];
  2801     $p_filename = $p_filedescr['filename'];
  3041 
  2802 
  3042     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a file");
       
  3043 
  2803 
  3044     // ----- Open the source file
  2804     // ----- Open the source file
  3045     if (($v_file = @fopen($p_filename, "rb")) == 0) {
  2805     if (($v_file = @fopen($p_filename, "rb")) == 0) {
  3046       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  2806       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  3047       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3048       return PclZip::errorCode();
  2807       return PclZip::errorCode();
  3049     }
  2808     }
  3050 
  2809 
  3051     // ----- Creates a compressed temporary file
  2810     // ----- Creates a compressed temporary file
  3052     $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  2811     $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  3053     if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
  2812     if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
  3054       fclose($v_file);
  2813       fclose($v_file);
  3055       PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  2814       PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  3056       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3057       return PclZip::errorCode();
  2815       return PclZip::errorCode();
  3058     }
  2816     }
  3059 
  2817 
  3060     // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  2818     // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3061     $v_size = filesize($p_filename);
  2819     $v_size = filesize($p_filename);
  3062     while ($v_size != 0) {
  2820     while ($v_size != 0) {
  3063       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2821       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3064       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read ".$v_read_size." bytes");
       
  3065       $v_buffer = @fread($v_file, $v_read_size);
  2822       $v_buffer = @fread($v_file, $v_read_size);
  3066       //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  2823       //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3067       @gzputs($v_file_compressed, $v_buffer, $v_read_size);
  2824       @gzputs($v_file_compressed, $v_buffer, $v_read_size);
  3068       $v_size -= $v_read_size;
  2825       $v_size -= $v_read_size;
  3069     }
  2826     }
  3071     // ----- Close the file
  2828     // ----- Close the file
  3072     @fclose($v_file);
  2829     @fclose($v_file);
  3073     @gzclose($v_file_compressed);
  2830     @gzclose($v_file_compressed);
  3074 
  2831 
  3075     // ----- Check the minimum file size
  2832     // ----- Check the minimum file size
  3076     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "gzip file size ".filesize($v_gzip_temp_name));
       
  3077     if (filesize($v_gzip_temp_name) < 18) {
  2833     if (filesize($v_gzip_temp_name) < 18) {
  3078       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
  2834       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
  3079       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3080       return PclZip::errorCode();
  2835       return PclZip::errorCode();
  3081     }
  2836     }
  3082 
  2837 
  3083     // ----- Extract the compressed attributes
  2838     // ----- Extract the compressed attributes
  3084     if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  2839     if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  3085       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  2840       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  3086       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3087       return PclZip::errorCode();
  2841       return PclZip::errorCode();
  3088     }
  2842     }
  3089 
  2843 
  3090     // ----- Read the gzip file header
  2844     // ----- Read the gzip file header
  3091     $v_binary_data = @fread($v_file_compressed, 10);
  2845     $v_binary_data = @fread($v_file_compressed, 10);
  3092     $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
  2846     $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
  3093 
  2847 
  3094     // ----- Check some parameters
  2848     // ----- Check some parameters
  3095     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[id1]='.bin2hex($v_data_header['id1']));
       
  3096     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[id2]='.bin2hex($v_data_header['id2']));
       
  3097     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[cm]='.bin2hex($v_data_header['cm']));
       
  3098     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[flag]='.bin2hex($v_data_header['flag']));
       
  3099     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[mtime]='.$v_data_header['mtime']);
       
  3100     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[xfl]='.bin2hex($v_data_header['xfl']));
       
  3101     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[os]='.bin2hex($v_data_header['os']));
       
  3102     $v_data_header['os'] = bin2hex($v_data_header['os']);
  2849     $v_data_header['os'] = bin2hex($v_data_header['os']);
  3103 
  2850 
  3104     // ----- Read the gzip file footer
  2851     // ----- Read the gzip file footer
  3105     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position after header ".ftell($v_file_compressed));
       
  3106     @fseek($v_file_compressed, filesize($v_gzip_temp_name)-8);
  2852     @fseek($v_file_compressed, filesize($v_gzip_temp_name)-8);
  3107     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position at beginning of footer ".ftell($v_file_compressed));
       
  3108     $v_binary_data = @fread($v_file_compressed, 8);
  2853     $v_binary_data = @fread($v_file_compressed, 8);
  3109     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position after footer ".ftell($v_file_compressed));
       
  3110     $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
  2854     $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
  3111 
  2855 
  3112     // ----- Set the attributes
  2856     // ----- Set the attributes
  3113     $p_header['compression'] = ord($v_data_header['cm']);
  2857     $p_header['compression'] = ord($v_data_header['cm']);
  3114     //$p_header['mtime'] = $v_data_header['mtime'];
  2858     //$p_header['mtime'] = $v_data_header['mtime'];
  3115     $p_header['crc'] = $v_data_footer['crc'];
  2859     $p_header['crc'] = $v_data_footer['crc'];
  3116     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Compressed size ".(filesize($v_gzip_temp_name)-18));
       
  3117     $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
  2860     $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
  3118 
  2861 
  3119     // ----- Close the file
  2862     // ----- Close the file
  3120     @fclose($v_file_compressed);
  2863     @fclose($v_file_compressed);
  3121 
  2864 
  3122     // ----- Call the header generation
  2865     // ----- Call the header generation
  3123     if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2866     if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  3124       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3125       return $v_result;
  2867       return $v_result;
  3126     }
  2868     }
  3127 
  2869 
  3128     // ----- Add the compressed data
  2870     // ----- Add the compressed data
  3129     if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0)
  2871     if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0)
  3130     {
  2872     {
  3131       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  2873       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  3132       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3133       return PclZip::errorCode();
  2874       return PclZip::errorCode();
  3134     }
  2875     }
  3135 
  2876 
  3136     // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  2877     // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3137     fseek($v_file_compressed, 10);
  2878     fseek($v_file_compressed, 10);
  3138     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position before reading compressed data ".ftell($v_file_compressed));
       
  3139     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, ' '.$p_header['compressed_size'].' bytes to read');
       
  3140     $v_size = $p_header['compressed_size'];
  2879     $v_size = $p_header['compressed_size'];
  3141     while ($v_size != 0)
  2880     while ($v_size != 0)
  3142     {
  2881     {
  3143       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2882       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3144       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read ".$v_read_size." bytes");
       
  3145       $v_buffer = @fread($v_file_compressed, $v_read_size);
  2883       $v_buffer = @fread($v_file_compressed, $v_read_size);
  3146       //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  2884       //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3147       @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  2885       @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  3148       $v_size -= $v_read_size;
  2886       $v_size -= $v_read_size;
  3149     }
  2887     }
  3151     // ----- Close the file
  2889     // ----- Close the file
  3152     @fclose($v_file_compressed);
  2890     @fclose($v_file_compressed);
  3153 
  2891 
  3154     // ----- Unlink the temporary file
  2892     // ----- Unlink the temporary file
  3155     @unlink($v_gzip_temp_name);
  2893     @unlink($v_gzip_temp_name);
  3156     
  2894 
  3157     // ----- Return
  2895     // ----- Return
  3158     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3159     return $v_result;
  2896     return $v_result;
  3160   }
  2897   }
  3161   // --------------------------------------------------------------------------------
  2898   // --------------------------------------------------------------------------------
  3162 
  2899 
  3163   // --------------------------------------------------------------------------------
  2900   // --------------------------------------------------------------------------------
  3168   // Parameters :
  2905   // Parameters :
  3169   // Return Values :
  2906   // Return Values :
  3170   // --------------------------------------------------------------------------------
  2907   // --------------------------------------------------------------------------------
  3171   function privCalculateStoredFilename(&$p_filedescr, &$p_options)
  2908   function privCalculateStoredFilename(&$p_filedescr, &$p_options)
  3172   {
  2909   {
  3173     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCalculateStoredFilename", "filename='".$p_filedescr['filename']."'");
       
  3174     $v_result=1;
  2910     $v_result=1;
  3175     
  2911 
  3176     // ----- Working variables
  2912     // ----- Working variables
  3177     $p_filename = $p_filedescr['filename'];
  2913     $p_filename = $p_filedescr['filename'];
  3178     if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
  2914     if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
  3179       $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
  2915       $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
  3180     }
  2916     }
  3185       $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
  2921       $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
  3186     }
  2922     }
  3187     else {
  2923     else {
  3188       $p_remove_dir = '';
  2924       $p_remove_dir = '';
  3189     }
  2925     }
  3190     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Remove path ='".$p_remove_dir."'");
       
  3191     if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  2926     if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  3192       $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  2927       $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  3193     }
  2928     }
  3194     else {
  2929     else {
  3195       $p_remove_all_dir = 0;
  2930       $p_remove_all_dir = 0;
  3196     }
  2931     }
       
  2932 
  3197 
  2933 
  3198     // ----- Look for full name change
  2934     // ----- Look for full name change
  3199     if (isset($p_filedescr['new_full_name'])) {
  2935     if (isset($p_filedescr['new_full_name'])) {
  3200       // ----- Remove drive letter if any
  2936       // ----- Remove drive letter if any
  3201       $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
  2937       $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
  3202       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing full name of '".$p_filename."' for '".$v_stored_filename."'");
  2938     }
  3203     }
  2939 
  3204     
       
  3205     // ----- Look for path and/or short name change
  2940     // ----- Look for path and/or short name change
  3206     else {
  2941     else {
  3207 
  2942 
  3208       // ----- Look for short name change
  2943       // ----- Look for short name change
  3209       // Its when we cahnge just the filename but not the path
  2944       // Its when we cahnge just the filename but not the path
  3212         $v_dir = '';
  2947         $v_dir = '';
  3213         if ($v_path_info['dirname'] != '') {
  2948         if ($v_path_info['dirname'] != '') {
  3214           $v_dir = $v_path_info['dirname'].'/';
  2949           $v_dir = $v_path_info['dirname'].'/';
  3215         }
  2950         }
  3216         $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
  2951         $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
  3217         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing short name of '".$p_filename."' for '".$v_stored_filename."'");
       
  3218       }
  2952       }
  3219       else {
  2953       else {
  3220         // ----- Calculate the stored filename
  2954         // ----- Calculate the stored filename
  3221         $v_stored_filename = $p_filename;
  2955         $v_stored_filename = $p_filename;
  3222       }
  2956       }
  3223 
  2957 
  3224       // ----- Look for all path to remove
  2958       // ----- Look for all path to remove
  3225       if ($p_remove_all_dir) {
  2959       if ($p_remove_all_dir) {
  3226         $v_stored_filename = basename($p_filename);
  2960         $v_stored_filename = basename($p_filename);
  3227         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove all path selected change '".$p_filename."' for '".$v_stored_filename."'");
       
  3228       }
  2961       }
  3229       // ----- Look for partial path remove
  2962       // ----- Look for partial path remove
  3230       else if ($p_remove_dir != "") {
  2963       else if ($p_remove_dir != "") {
  3231         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Partial path to remove");
       
  3232         if (substr($p_remove_dir, -1) != '/')
  2964         if (substr($p_remove_dir, -1) != '/')
  3233           $p_remove_dir .= "/";
  2965           $p_remove_dir .= "/";
  3234 
  2966 
  3235         if (   (substr($p_filename, 0, 2) == "./")
  2967         if (   (substr($p_filename, 0, 2) == "./")
  3236             || (substr($p_remove_dir, 0, 2) == "./")) {
  2968             || (substr($p_remove_dir, 0, 2) == "./")) {
  3237             
  2969 
  3238           if (   (substr($p_filename, 0, 2) == "./")
  2970           if (   (substr($p_filename, 0, 2) == "./")
  3239               && (substr($p_remove_dir, 0, 2) != "./")) {
  2971               && (substr($p_remove_dir, 0, 2) != "./")) {
  3240             $p_remove_dir = "./".$p_remove_dir;
  2972             $p_remove_dir = "./".$p_remove_dir;
  3241           }
  2973           }
  3242           if (   (substr($p_filename, 0, 2) != "./")
  2974           if (   (substr($p_filename, 0, 2) != "./")
  3248         $v_compare = PclZipUtilPathInclusion($p_remove_dir,
  2980         $v_compare = PclZipUtilPathInclusion($p_remove_dir,
  3249                                              $v_stored_filename);
  2981                                              $v_stored_filename);
  3250         if ($v_compare > 0) {
  2982         if ($v_compare > 0) {
  3251           if ($v_compare == 2) {
  2983           if ($v_compare == 2) {
  3252             $v_stored_filename = "";
  2984             $v_stored_filename = "";
  3253             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Path to remove is the current folder");
       
  3254           }
  2985           }
  3255           else {
  2986           else {
  3256             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove path '$p_remove_dir' in file '$v_stored_filename'");
       
  3257             $v_stored_filename = substr($v_stored_filename,
  2987             $v_stored_filename = substr($v_stored_filename,
  3258                                         strlen($p_remove_dir));
  2988                                         strlen($p_remove_dir));
  3259             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Result is '$v_stored_filename'");
  2989           }
  3260           }
  2990         }
  3261         }
  2991       }
  3262       }
  2992 
  3263       
       
  3264       // ----- Remove drive letter if any
  2993       // ----- Remove drive letter if any
  3265       $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
  2994       $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
  3266       
  2995 
  3267       // ----- Look for path to add
  2996       // ----- Look for path to add
  3268       if ($p_add_dir != "") {
  2997       if ($p_add_dir != "") {
  3269         if (substr($p_add_dir, -1) == "/")
  2998         if (substr($p_add_dir, -1) == "/")
  3270           $v_stored_filename = $p_add_dir.$v_stored_filename;
  2999           $v_stored_filename = $p_add_dir.$v_stored_filename;
  3271         else
  3000         else
  3272           $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  3001           $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  3273         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
       
  3274       }
  3002       }
  3275     }
  3003     }
  3276 
  3004 
  3277     // ----- Filename (reduce the path of stored name)
  3005     // ----- Filename (reduce the path of stored name)
  3278     $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
  3006     $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
  3279     $p_filedescr['stored_filename'] = $v_stored_filename;
  3007     $p_filedescr['stored_filename'] = $v_stored_filename;
  3280     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Stored filename will be '".$p_filedescr['stored_filename']."', strlen ".strlen($p_filedescr['stored_filename']));
  3008 
  3281     
       
  3282     // ----- Return
  3009     // ----- Return
  3283     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3284     return $v_result;
  3010     return $v_result;
  3285   }
  3011   }
  3286   // --------------------------------------------------------------------------------
  3012   // --------------------------------------------------------------------------------
  3287 
  3013 
  3288   // --------------------------------------------------------------------------------
  3014   // --------------------------------------------------------------------------------
  3291   // Parameters :
  3017   // Parameters :
  3292   // Return Values :
  3018   // Return Values :
  3293   // --------------------------------------------------------------------------------
  3019   // --------------------------------------------------------------------------------
  3294   function privWriteFileHeader(&$p_header)
  3020   function privWriteFileHeader(&$p_header)
  3295   {
  3021   {
  3296     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
       
  3297     $v_result=1;
  3022     $v_result=1;
  3298 
  3023 
  3299     // ----- Store the offset position of the file
  3024     // ----- Store the offset position of the file
  3300     $p_header['offset'] = ftell($this->zip_fd);
  3025     $p_header['offset'] = ftell($this->zip_fd);
  3301     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'File offset of the header :'.$p_header['offset']);
       
  3302 
  3026 
  3303     // ----- Transform UNIX mtime to DOS format mdate/mtime
  3027     // ----- Transform UNIX mtime to DOS format mdate/mtime
  3304     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
       
  3305     $v_date = getdate($p_header['mtime']);
  3028     $v_date = getdate($p_header['mtime']);
  3306     $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  3029     $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  3307     $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  3030     $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  3308 
  3031 
  3309     // ----- Packed data
  3032     // ----- Packed data
  3327     {
  3050     {
  3328       fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  3051       fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  3329     }
  3052     }
  3330 
  3053 
  3331     // ----- Return
  3054     // ----- Return
  3332     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3333     return $v_result;
  3055     return $v_result;
  3334   }
  3056   }
  3335   // --------------------------------------------------------------------------------
  3057   // --------------------------------------------------------------------------------
  3336 
  3058 
  3337   // --------------------------------------------------------------------------------
  3059   // --------------------------------------------------------------------------------
  3340   // Parameters :
  3062   // Parameters :
  3341   // Return Values :
  3063   // Return Values :
  3342   // --------------------------------------------------------------------------------
  3064   // --------------------------------------------------------------------------------
  3343   function privWriteCentralFileHeader(&$p_header)
  3065   function privWriteCentralFileHeader(&$p_header)
  3344   {
  3066   {
  3345     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
       
  3346     $v_result=1;
  3067     $v_result=1;
  3347 
  3068 
  3348     // TBC
  3069     // TBC
  3349     //for(reset($p_header); $key = key($p_header); next($p_header)) {
  3070     //for(reset($p_header); $key = key($p_header); next($p_header)) {
  3350     //  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "header[$key] = ".$p_header[$key]);
       
  3351     //}
  3071     //}
  3352 
  3072 
  3353     // ----- Transform UNIX mtime to DOS format mdate/mtime
  3073     // ----- Transform UNIX mtime to DOS format mdate/mtime
  3354     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
       
  3355     $v_date = getdate($p_header['mtime']);
  3074     $v_date = getdate($p_header['mtime']);
  3356     $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  3075     $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  3357     $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  3076     $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  3358 
  3077 
  3359     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment size : \''.$p_header['comment_len'].'\'');
       
  3360 
  3078 
  3361     // ----- Packed data
  3079     // ----- Packed data
  3362     $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
  3080     $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
  3363 	                      $p_header['version'], $p_header['version_extracted'],
  3081 	                      $p_header['version'], $p_header['version_extracted'],
  3364                           $p_header['flag'], $p_header['compression'],
  3082                           $p_header['flag'], $p_header['compression'],
  3385     {
  3103     {
  3386       fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
  3104       fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
  3387     }
  3105     }
  3388 
  3106 
  3389     // ----- Return
  3107     // ----- Return
  3390     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3391     return $v_result;
  3108     return $v_result;
  3392   }
  3109   }
  3393   // --------------------------------------------------------------------------------
  3110   // --------------------------------------------------------------------------------
  3394 
  3111 
  3395   // --------------------------------------------------------------------------------
  3112   // --------------------------------------------------------------------------------
  3398   // Parameters :
  3115   // Parameters :
  3399   // Return Values :
  3116   // Return Values :
  3400   // --------------------------------------------------------------------------------
  3117   // --------------------------------------------------------------------------------
  3401   function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  3118   function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  3402   {
  3119   {
  3403     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralHeader", 'nb_entries='.$p_nb_entries.', size='.$p_size.', offset='.$p_offset.', comment="'.$p_comment.'"');
       
  3404     $v_result=1;
  3120     $v_result=1;
  3405 
  3121 
  3406     // ----- Packed data
  3122     // ----- Packed data
  3407     $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
  3123     $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
  3408 	                      $p_nb_entries, $p_size,
  3124 	                      $p_nb_entries, $p_size,
  3416     {
  3132     {
  3417       fputs($this->zip_fd, $p_comment, strlen($p_comment));
  3133       fputs($this->zip_fd, $p_comment, strlen($p_comment));
  3418     }
  3134     }
  3419 
  3135 
  3420     // ----- Return
  3136     // ----- Return
  3421     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3422     return $v_result;
  3137     return $v_result;
  3423   }
  3138   }
  3424   // --------------------------------------------------------------------------------
  3139   // --------------------------------------------------------------------------------
  3425 
  3140 
  3426   // --------------------------------------------------------------------------------
  3141   // --------------------------------------------------------------------------------
  3429   // Parameters :
  3144   // Parameters :
  3430   // Return Values :
  3145   // Return Values :
  3431   // --------------------------------------------------------------------------------
  3146   // --------------------------------------------------------------------------------
  3432   function privList(&$p_list)
  3147   function privList(&$p_list)
  3433   {
  3148   {
  3434     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privList", "list");
       
  3435     $v_result=1;
  3149     $v_result=1;
  3436 
  3150 
  3437     // ----- Magic quotes trick
  3151     // ----- Magic quotes trick
  3438     $this->privDisableMagicQuotes();
  3152     $this->privDisableMagicQuotes();
  3439 
  3153 
  3440     // ----- Open the zip file
  3154     // ----- Open the zip file
  3441     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  3442     if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  3155     if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  3443     {
  3156     {
  3444       // ----- Magic quotes trick
  3157       // ----- Magic quotes trick
  3445       $this->privSwapBackMagicQuotes();
  3158       $this->privSwapBackMagicQuotes();
  3446       
  3159 
  3447       // ----- Error log
  3160       // ----- Error log
  3448       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  3161       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  3449 
  3162 
  3450       // ----- Return
  3163       // ----- Return
  3451       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3452       return PclZip::errorCode();
  3164       return PclZip::errorCode();
  3453     }
  3165     }
  3454 
  3166 
  3455     // ----- Read the central directory informations
  3167     // ----- Read the central directory informations
  3456     $v_central_dir = array();
  3168     $v_central_dir = array();
  3457     if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  3169     if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  3458     {
  3170     {
  3459       $this->privSwapBackMagicQuotes();
  3171       $this->privSwapBackMagicQuotes();
  3460       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3461       return $v_result;
  3172       return $v_result;
  3462     }
  3173     }
  3463 
  3174 
  3464     // ----- Go to beginning of Central Dir
  3175     // ----- Go to beginning of Central Dir
  3465     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Offset : ".$v_central_dir['offset']."'");
       
  3466     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
       
  3467     @rewind($this->zip_fd);
  3176     @rewind($this->zip_fd);
  3468     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
       
  3469     if (@fseek($this->zip_fd, $v_central_dir['offset']))
  3177     if (@fseek($this->zip_fd, $v_central_dir['offset']))
  3470     {
  3178     {
  3471       $this->privSwapBackMagicQuotes();
  3179       $this->privSwapBackMagicQuotes();
  3472 
  3180 
  3473       // ----- Error log
  3181       // ----- Error log
  3474       PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3182       PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3475 
  3183 
  3476       // ----- Return
  3184       // ----- Return
  3477       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3478       return PclZip::errorCode();
  3185       return PclZip::errorCode();
  3479     }
  3186     }
  3480     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
       
  3481 
  3187 
  3482     // ----- Read each entry
  3188     // ----- Read each entry
  3483     for ($i=0; $i<$v_central_dir['entries']; $i++)
  3189     for ($i=0; $i<$v_central_dir['entries']; $i++)
  3484     {
  3190     {
  3485       // ----- Read the file header
  3191       // ----- Read the file header
  3486       if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  3192       if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  3487       {
  3193       {
  3488         $this->privSwapBackMagicQuotes();
  3194         $this->privSwapBackMagicQuotes();
  3489         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3490         return $v_result;
  3195         return $v_result;
  3491       }
  3196       }
  3492       $v_header['index'] = $i;
  3197       $v_header['index'] = $i;
  3493 
  3198 
  3494       // ----- Get the only interesting attributes
  3199       // ----- Get the only interesting attributes
  3501 
  3206 
  3502     // ----- Magic quotes trick
  3207     // ----- Magic quotes trick
  3503     $this->privSwapBackMagicQuotes();
  3208     $this->privSwapBackMagicQuotes();
  3504 
  3209 
  3505     // ----- Return
  3210     // ----- Return
  3506     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3507     return $v_result;
  3211     return $v_result;
  3508   }
  3212   }
  3509   // --------------------------------------------------------------------------------
  3213   // --------------------------------------------------------------------------------
  3510 
  3214 
  3511   // --------------------------------------------------------------------------------
  3215   // --------------------------------------------------------------------------------
  3527   // Parameters :
  3231   // Parameters :
  3528   // Return Values :
  3232   // Return Values :
  3529   // --------------------------------------------------------------------------------
  3233   // --------------------------------------------------------------------------------
  3530   function privConvertHeader2FileInfo($p_header, &$p_info)
  3234   function privConvertHeader2FileInfo($p_header, &$p_info)
  3531   {
  3235   {
  3532     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privConvertHeader2FileInfo", "Filename='".$p_header['filename']."'");
       
  3533     $v_result=1;
  3236     $v_result=1;
  3534 
  3237 
  3535     // ----- Get the interesting attributes
  3238     // ----- Get the interesting attributes
  3536     $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
  3239     $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
  3537     $p_info['filename'] = $v_temp_path;
  3240     $p_info['filename'] = $v_temp_path;
  3545     $p_info['index'] = $p_header['index'];
  3248     $p_info['index'] = $p_header['index'];
  3546     $p_info['status'] = $p_header['status'];
  3249     $p_info['status'] = $p_header['status'];
  3547     $p_info['crc'] = $p_header['crc'];
  3250     $p_info['crc'] = $p_header['crc'];
  3548 
  3251 
  3549     // ----- Return
  3252     // ----- Return
  3550     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3551     return $v_result;
  3253     return $v_result;
  3552   }
  3254   }
  3553   // --------------------------------------------------------------------------------
  3255   // --------------------------------------------------------------------------------
  3554 
  3256 
  3555   // --------------------------------------------------------------------------------
  3257   // --------------------------------------------------------------------------------
  3568   // Return Values :
  3270   // Return Values :
  3569   //   1 on success,0 or less on error (see error code list)
  3271   //   1 on success,0 or less on error (see error code list)
  3570   // --------------------------------------------------------------------------------
  3272   // --------------------------------------------------------------------------------
  3571   function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  3273   function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  3572   {
  3274   {
  3573     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privExtractByRule", "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
       
  3574     $v_result=1;
  3275     $v_result=1;
  3575 
  3276 
  3576     // ----- Magic quotes trick
  3277     // ----- Magic quotes trick
  3577     $this->privDisableMagicQuotes();
  3278     $this->privDisableMagicQuotes();
  3578 
  3279 
  3587     if (($p_path != "./") && ($p_path != "/"))
  3288     if (($p_path != "./") && ($p_path != "/"))
  3588     {
  3289     {
  3589       // ----- Look for the path end '/'
  3290       // ----- Look for the path end '/'
  3590       while (substr($p_path, -1) == "/")
  3291       while (substr($p_path, -1) == "/")
  3591       {
  3292       {
  3592         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
       
  3593         $p_path = substr($p_path, 0, strlen($p_path)-1);
  3293         $p_path = substr($p_path, 0, strlen($p_path)-1);
  3594         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
       
  3595       }
  3294       }
  3596     }
  3295     }
  3597 
  3296 
  3598     // ----- Look for path to remove format (should end by /)
  3297     // ----- Look for path to remove format (should end by /)
  3599     if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
  3298     if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
  3601       $p_remove_path .= '/';
  3300       $p_remove_path .= '/';
  3602     }
  3301     }
  3603     $p_remove_path_size = strlen($p_remove_path);
  3302     $p_remove_path_size = strlen($p_remove_path);
  3604 
  3303 
  3605     // ----- Open the zip file
  3304     // ----- Open the zip file
  3606     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  3607     if (($v_result = $this->privOpenFd('rb')) != 1)
  3305     if (($v_result = $this->privOpenFd('rb')) != 1)
  3608     {
  3306     {
  3609       $this->privSwapBackMagicQuotes();
  3307       $this->privSwapBackMagicQuotes();
  3610       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3611       return $v_result;
  3308       return $v_result;
  3612     }
  3309     }
  3613 
  3310 
  3614     // ----- Read the central directory informations
  3311     // ----- Read the central directory informations
  3615     $v_central_dir = array();
  3312     $v_central_dir = array();
  3617     {
  3314     {
  3618       // ----- Close the zip file
  3315       // ----- Close the zip file
  3619       $this->privCloseFd();
  3316       $this->privCloseFd();
  3620       $this->privSwapBackMagicQuotes();
  3317       $this->privSwapBackMagicQuotes();
  3621 
  3318 
  3622       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3623       return $v_result;
  3319       return $v_result;
  3624     }
  3320     }
  3625 
  3321 
  3626     // ----- Start at beginning of Central Dir
  3322     // ----- Start at beginning of Central Dir
  3627     $v_pos_entry = $v_central_dir['offset'];
  3323     $v_pos_entry = $v_central_dir['offset'];
  3628 
  3324 
  3629     // ----- Read each entry
  3325     // ----- Read each entry
  3630     $j_start = 0;
  3326     $j_start = 0;
  3631     for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  3327     for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  3632     {
  3328     {
  3633       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry : '$i'");
       
  3634 
  3329 
  3635       // ----- Read next Central dir entry
  3330       // ----- Read next Central dir entry
  3636       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position before rewind : ".ftell($this->zip_fd)."'");
       
  3637       @rewind($this->zip_fd);
  3331       @rewind($this->zip_fd);
  3638       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position after rewind : ".ftell($this->zip_fd)."'");
       
  3639       if (@fseek($this->zip_fd, $v_pos_entry))
  3332       if (@fseek($this->zip_fd, $v_pos_entry))
  3640       {
  3333       {
  3641         // ----- Close the zip file
  3334         // ----- Close the zip file
  3642         $this->privCloseFd();
  3335         $this->privCloseFd();
  3643         $this->privSwapBackMagicQuotes();
  3336         $this->privSwapBackMagicQuotes();
  3644 
  3337 
  3645         // ----- Error log
  3338         // ----- Error log
  3646         PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3339         PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3647 
  3340 
  3648         // ----- Return
  3341         // ----- Return
  3649         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3650         return PclZip::errorCode();
  3342         return PclZip::errorCode();
  3651       }
  3343       }
  3652       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position after fseek : ".ftell($this->zip_fd)."'");
       
  3653 
  3344 
  3654       // ----- Read the file header
  3345       // ----- Read the file header
  3655       $v_header = array();
  3346       $v_header = array();
  3656       if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  3347       if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  3657       {
  3348       {
  3658         // ----- Close the zip file
  3349         // ----- Close the zip file
  3659         $this->privCloseFd();
  3350         $this->privCloseFd();
  3660         $this->privSwapBackMagicQuotes();
  3351         $this->privSwapBackMagicQuotes();
  3661 
  3352 
  3662         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3663         return $v_result;
  3353         return $v_result;
  3664       }
  3354       }
  3665 
  3355 
  3666       // ----- Store the index
  3356       // ----- Store the index
  3667       $v_header['index'] = $i;
  3357       $v_header['index'] = $i;
  3673       $v_extract = false;
  3363       $v_extract = false;
  3674 
  3364 
  3675       // ----- Look for extract by name rule
  3365       // ----- Look for extract by name rule
  3676       if (   (isset($p_options[PCLZIP_OPT_BY_NAME]))
  3366       if (   (isset($p_options[PCLZIP_OPT_BY_NAME]))
  3677           && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  3367           && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  3678           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
       
  3679 
  3368 
  3680           // ----- Look if the filename is in the list
  3369           // ----- Look if the filename is in the list
  3681           for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
  3370           for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
  3682               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
       
  3683 
  3371 
  3684               // ----- Look for a directory
  3372               // ----- Look for a directory
  3685               if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  3373               if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  3686                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
       
  3687 
  3374 
  3688                   // ----- Look if the directory is in the filename path
  3375                   // ----- Look if the directory is in the filename path
  3689                   if (   (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  3376                   if (   (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  3690                       && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  3377                       && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  3691                       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
       
  3692                       $v_extract = true;
  3378                       $v_extract = true;
  3693                   }
  3379                   }
  3694               }
  3380               }
  3695               // ----- Look for a filename
  3381               // ----- Look for a filename
  3696               elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  3382               elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  3697                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
       
  3698                   $v_extract = true;
  3383                   $v_extract = true;
  3699               }
  3384               }
  3700           }
  3385           }
  3701       }
  3386       }
  3702 
  3387 
  3703       // ----- Look for extract by ereg rule
  3388       // ----- Look for extract by ereg rule
       
  3389       // ereg() is deprecated with PHP 5.3
       
  3390       /*
  3704       else if (   (isset($p_options[PCLZIP_OPT_BY_EREG]))
  3391       else if (   (isset($p_options[PCLZIP_OPT_BY_EREG]))
  3705                && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  3392                && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  3706           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
       
  3707 
  3393 
  3708           if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
  3394           if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
  3709               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
       
  3710               $v_extract = true;
  3395               $v_extract = true;
  3711           }
  3396           }
  3712       }
  3397       }
       
  3398       */
  3713 
  3399 
  3714       // ----- Look for extract by preg rule
  3400       // ----- Look for extract by preg rule
  3715       else if (   (isset($p_options[PCLZIP_OPT_BY_PREG]))
  3401       else if (   (isset($p_options[PCLZIP_OPT_BY_PREG]))
  3716                && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  3402                && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  3717           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
       
  3718 
  3403 
  3719           if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
  3404           if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
  3720               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
       
  3721               $v_extract = true;
  3405               $v_extract = true;
  3722           }
  3406           }
  3723       }
  3407       }
  3724 
  3408 
  3725       // ----- Look for extract by index rule
  3409       // ----- Look for extract by index rule
  3726       else if (   (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  3410       else if (   (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  3727                && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  3411                && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  3728           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
  3412 
  3729           
       
  3730           // ----- Look if the index is in the list
  3413           // ----- Look if the index is in the list
  3731           for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
  3414           for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
  3732               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look if index '$i' is in [".$p_options[PCLZIP_OPT_BY_INDEX][$j]['start'].",".$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']."]");
       
  3733 
  3415 
  3734               if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  3416               if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  3735                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
       
  3736                   $v_extract = true;
  3417                   $v_extract = true;
  3737               }
  3418               }
  3738               if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  3419               if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  3739                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
       
  3740                   $j_start = $j+1;
  3420                   $j_start = $j+1;
  3741               }
  3421               }
  3742 
  3422 
  3743               if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  3423               if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  3744                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
       
  3745                   break;
  3424                   break;
  3746               }
  3425               }
  3747           }
  3426           }
  3748       }
  3427       }
  3749 
  3428 
  3750       // ----- Look for no rule, which means extract all the archive
  3429       // ----- Look for no rule, which means extract all the archive
  3751       else {
  3430       else {
  3752           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with no rule (extract all)");
       
  3753           $v_extract = true;
  3431           $v_extract = true;
  3754       }
  3432       }
  3755 
  3433 
  3756 	  // ----- Check compression method
  3434 	  // ----- Check compression method
  3757 	  if (   ($v_extract)
  3435 	  if (   ($v_extract)
  3758 	      && (   ($v_header['compression'] != 8)
  3436 	      && (   ($v_header['compression'] != 8)
  3759 		      && ($v_header['compression'] != 0))) {
  3437 		      && ($v_header['compression'] != 0))) {
  3760           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported compression method (".$v_header['compression'].")");
       
  3761           $v_header['status'] = 'unsupported_compression';
  3438           $v_header['status'] = 'unsupported_compression';
  3762 
  3439 
  3763           // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3440           // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3764           if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3441           if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3765 		      && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3442 		      && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3766               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
       
  3767 
  3443 
  3768               $this->privSwapBackMagicQuotes();
  3444               $this->privSwapBackMagicQuotes();
  3769               
  3445 
  3770               PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
  3446               PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
  3771 			                       "Filename '".$v_header['stored_filename']."' is "
  3447 			                       "Filename '".$v_header['stored_filename']."' is "
  3772 				  	    	  	   ."compressed by an unsupported compression "
  3448 				  	    	  	   ."compressed by an unsupported compression "
  3773 				  	    	  	   ."method (".$v_header['compression'].") ");
  3449 				  	    	  	   ."method (".$v_header['compression'].") ");
  3774 
  3450 
  3775               //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3776               return PclZip::errorCode();
  3451               return PclZip::errorCode();
  3777 		  }
  3452 		  }
  3778 	  }
  3453 	  }
  3779 	  
  3454 
  3780 	  // ----- Check encrypted files
  3455 	  // ----- Check encrypted files
  3781 	  if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
  3456 	  if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
  3782           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unsupported file encryption");
       
  3783           $v_header['status'] = 'unsupported_encryption';
  3457           $v_header['status'] = 'unsupported_encryption';
  3784 
  3458 
  3785           // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3459           // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3786           if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3460           if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3787 		      && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3461 		      && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3788               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
       
  3789 
  3462 
  3790               $this->privSwapBackMagicQuotes();
  3463               $this->privSwapBackMagicQuotes();
  3791 
  3464 
  3792               PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
  3465               PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
  3793 			                       "Unsupported encryption for "
  3466 			                       "Unsupported encryption for "
  3794 				  	    	  	   ." filename '".$v_header['stored_filename']
  3467 				  	    	  	   ." filename '".$v_header['stored_filename']
  3795 								   ."'");
  3468 								   ."'");
  3796 
  3469 
  3797               //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3798               return PclZip::errorCode();
  3470               return PclZip::errorCode();
  3799 		  }
  3471 		  }
  3800     }
  3472     }
  3801 
  3473 
  3802       // ----- Look for real extraction
  3474       // ----- Look for real extraction
  3803       if (($v_extract) && ($v_header['status'] != 'ok')) {
  3475       if (($v_extract) && ($v_header['status'] != 'ok')) {
  3804           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "No need for extract");
       
  3805           $v_result = $this->privConvertHeader2FileInfo($v_header,
  3476           $v_result = $this->privConvertHeader2FileInfo($v_header,
  3806 		                                        $p_file_list[$v_nb_extracted++]);
  3477 		                                        $p_file_list[$v_nb_extracted++]);
  3807           if ($v_result != 1) {
  3478           if ($v_result != 1) {
  3808               $this->privCloseFd();
  3479               $this->privCloseFd();
  3809               $this->privSwapBackMagicQuotes();
  3480               $this->privSwapBackMagicQuotes();
  3810               //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3811               return $v_result;
  3481               return $v_result;
  3812           }
  3482           }
  3813 
  3483 
  3814           $v_extract = false;
  3484           $v_extract = false;
  3815       }
  3485       }
  3816       
  3486 
  3817       // ----- Look for real extraction
  3487       // ----- Look for real extraction
  3818       if ($v_extract)
  3488       if ($v_extract)
  3819       {
  3489       {
  3820         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file '".$v_header['filename']."', index '$i'");
       
  3821 
  3490 
  3822         // ----- Go to the file position
  3491         // ----- Go to the file position
  3823         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
       
  3824         @rewind($this->zip_fd);
  3492         @rewind($this->zip_fd);
  3825         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
       
  3826         if (@fseek($this->zip_fd, $v_header['offset']))
  3493         if (@fseek($this->zip_fd, $v_header['offset']))
  3827         {
  3494         {
  3828           // ----- Close the zip file
  3495           // ----- Close the zip file
  3829           $this->privCloseFd();
  3496           $this->privCloseFd();
  3830 
  3497 
  3832 
  3499 
  3833           // ----- Error log
  3500           // ----- Error log
  3834           PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3501           PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3835 
  3502 
  3836           // ----- Return
  3503           // ----- Return
  3837           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  3838           return PclZip::errorCode();
  3504           return PclZip::errorCode();
  3839         }
  3505         }
  3840         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
       
  3841 
  3506 
  3842         // ----- Look for extraction as string
  3507         // ----- Look for extraction as string
  3843         if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
  3508         if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
  3844 
  3509 
       
  3510           $v_string = '';
       
  3511 
  3845           // ----- Extracting the file
  3512           // ----- Extracting the file
  3846           $v_result1 = $this->privExtractFileAsString($v_header, $v_string);
  3513           $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
  3847           if ($v_result1 < 1) {
  3514           if ($v_result1 < 1) {
  3848             $this->privCloseFd();
  3515             $this->privCloseFd();
  3849             $this->privSwapBackMagicQuotes();
  3516             $this->privSwapBackMagicQuotes();
  3850             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
       
  3851             return $v_result1;
  3517             return $v_result1;
  3852           }
  3518           }
  3853 
  3519 
  3854           // ----- Get the only interesting attributes
  3520           // ----- Get the only interesting attributes
  3855           if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
  3521           if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
  3856           {
  3522           {
  3857             // ----- Close the zip file
  3523             // ----- Close the zip file
  3858             $this->privCloseFd();
  3524             $this->privCloseFd();
  3859             $this->privSwapBackMagicQuotes();
  3525             $this->privSwapBackMagicQuotes();
  3860 
  3526 
  3861             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3862             return $v_result;
  3527             return $v_result;
  3863           }
  3528           }
  3864 
  3529 
  3865           // ----- Set the file content
  3530           // ----- Set the file content
  3866           $p_file_list[$v_nb_extracted]['content'] = $v_string;
  3531           $p_file_list[$v_nb_extracted]['content'] = $v_string;
  3867 
  3532 
  3868           // ----- Next extracted file
  3533           // ----- Next extracted file
  3869           $v_nb_extracted++;
  3534           $v_nb_extracted++;
  3870           
  3535 
  3871           // ----- Look for user callback abort
  3536           // ----- Look for user callback abort
  3872           if ($v_result1 == 2) {
  3537           if ($v_result1 == 2) {
  3873           	break;
  3538           	break;
  3874           }
  3539           }
  3875         }
  3540         }
  3879           // ----- Extracting the file in standard output
  3544           // ----- Extracting the file in standard output
  3880           $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
  3545           $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
  3881           if ($v_result1 < 1) {
  3546           if ($v_result1 < 1) {
  3882             $this->privCloseFd();
  3547             $this->privCloseFd();
  3883             $this->privSwapBackMagicQuotes();
  3548             $this->privSwapBackMagicQuotes();
  3884             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
       
  3885             return $v_result1;
  3549             return $v_result1;
  3886           }
  3550           }
  3887 
  3551 
  3888           // ----- Get the only interesting attributes
  3552           // ----- Get the only interesting attributes
  3889           if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  3553           if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  3890             $this->privCloseFd();
  3554             $this->privCloseFd();
  3891             $this->privSwapBackMagicQuotes();
  3555             $this->privSwapBackMagicQuotes();
  3892             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3893             return $v_result;
  3556             return $v_result;
  3894           }
  3557           }
  3895 
  3558 
  3896           // ----- Look for user callback abort
  3559           // ----- Look for user callback abort
  3897           if ($v_result1 == 2) {
  3560           if ($v_result1 == 2) {
  3906 											  $p_remove_all_path,
  3569 											  $p_remove_all_path,
  3907 											  $p_options);
  3570 											  $p_options);
  3908           if ($v_result1 < 1) {
  3571           if ($v_result1 < 1) {
  3909             $this->privCloseFd();
  3572             $this->privCloseFd();
  3910             $this->privSwapBackMagicQuotes();
  3573             $this->privSwapBackMagicQuotes();
  3911             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result1);
       
  3912             return $v_result1;
  3574             return $v_result1;
  3913           }
  3575           }
  3914 
  3576 
  3915           // ----- Get the only interesting attributes
  3577           // ----- Get the only interesting attributes
  3916           if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
  3578           if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
  3917           {
  3579           {
  3918             // ----- Close the zip file
  3580             // ----- Close the zip file
  3919             $this->privCloseFd();
  3581             $this->privCloseFd();
  3920             $this->privSwapBackMagicQuotes();
  3582             $this->privSwapBackMagicQuotes();
  3921 
  3583 
  3922             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3923             return $v_result;
  3584             return $v_result;
  3924           }
  3585           }
  3925 
  3586 
  3926           // ----- Look for user callback abort
  3587           // ----- Look for user callback abort
  3927           if ($v_result1 == 2) {
  3588           if ($v_result1 == 2) {
  3934     // ----- Close the zip file
  3595     // ----- Close the zip file
  3935     $this->privCloseFd();
  3596     $this->privCloseFd();
  3936     $this->privSwapBackMagicQuotes();
  3597     $this->privSwapBackMagicQuotes();
  3937 
  3598 
  3938     // ----- Return
  3599     // ----- Return
  3939     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3940     return $v_result;
  3600     return $v_result;
  3941   }
  3601   }
  3942   // --------------------------------------------------------------------------------
  3602   // --------------------------------------------------------------------------------
  3943 
  3603 
  3944   // --------------------------------------------------------------------------------
  3604   // --------------------------------------------------------------------------------
  3950   // 1 : ... ?
  3610   // 1 : ... ?
  3951   // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
  3611   // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
  3952   // --------------------------------------------------------------------------------
  3612   // --------------------------------------------------------------------------------
  3953   function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  3613   function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  3954   {
  3614   {
  3955     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFile', "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
       
  3956     $v_result=1;
  3615     $v_result=1;
  3957 
  3616 
  3958     // ----- Read the file header
  3617     // ----- Read the file header
  3959     if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  3618     if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  3960     {
  3619     {
  3961       // ----- Return
  3620       // ----- Return
  3962       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3963       return $v_result;
  3621       return $v_result;
  3964     }
  3622     }
  3965 
  3623 
  3966     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
       
  3967 
  3624 
  3968     // ----- Check that the file header is coherent with $p_entry info
  3625     // ----- Check that the file header is coherent with $p_entry info
  3969     if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  3626     if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  3970         // TBC
  3627         // TBC
  3971     }
  3628     }
  3972 
  3629 
  3973     // ----- Look for all path to remove
  3630     // ----- Look for all path to remove
  3974     if ($p_remove_all_path == true) {
  3631     if ($p_remove_all_path == true) {
  3975         // ----- Look for folder entry that not need to be extracted
  3632         // ----- Look for folder entry that not need to be extracted
  3976         if (($p_entry['external']&0x00000010)==0x00000010) {
  3633         if (($p_entry['external']&0x00000010)==0x00000010) {
  3977             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The entry is a folder : need to be filtered");
       
  3978 
  3634 
  3979             $p_entry['status'] = "filtered";
  3635             $p_entry['status'] = "filtered";
  3980 
  3636 
  3981             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  3982             return $v_result;
  3637             return $v_result;
  3983         }
  3638         }
  3984 
  3639 
  3985         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "All path is removed");
       
  3986         // ----- Get the basename of the path
  3640         // ----- Get the basename of the path
  3987         $p_entry['filename'] = basename($p_entry['filename']);
  3641         $p_entry['filename'] = basename($p_entry['filename']);
  3988     }
  3642     }
  3989 
  3643 
  3990     // ----- Look for path to remove
  3644     // ----- Look for path to remove
  3991     else if ($p_remove_path != "")
  3645     else if ($p_remove_path != "")
  3992     {
  3646     {
  3993       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look for some path to remove");
       
  3994       if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
  3647       if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
  3995       {
  3648       {
  3996         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The folder is the same as the removed path '".$p_entry['filename']."'");
       
  3997 
  3649 
  3998         // ----- Change the file status
  3650         // ----- Change the file status
  3999         $p_entry['status'] = "filtered";
  3651         $p_entry['status'] = "filtered";
  4000 
  3652 
  4001         // ----- Return
  3653         // ----- Return
  4002         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4003         return $v_result;
  3654         return $v_result;
  4004       }
  3655       }
  4005 
  3656 
  4006       $p_remove_path_size = strlen($p_remove_path);
  3657       $p_remove_path_size = strlen($p_remove_path);
  4007       if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
  3658       if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
  4008       {
  3659       {
  4009         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found path '$p_remove_path' to remove in file '".$p_entry['filename']."'");
       
  4010 
  3660 
  4011         // ----- Remove the path
  3661         // ----- Remove the path
  4012         $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  3662         $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  4013 
  3663 
  4014         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Resulting file is '".$p_entry['filename']."'");
       
  4015       }
  3664       }
  4016     }
  3665     }
  4017 
  3666 
  4018     // ----- Add the path
  3667     // ----- Add the path
  4019     if ($p_path != '') {
  3668     if ($p_path != '') {
  4020       $p_entry['filename'] = $p_path."/".$p_entry['filename'];
  3669       $p_entry['filename'] = $p_path."/".$p_entry['filename'];
  4021     }
  3670     }
  4022     
  3671 
  4023     // ----- Check a base_dir_restriction
  3672     // ----- Check a base_dir_restriction
  4024     if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
  3673     if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
  4025       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Check the extract directory restriction");
       
  4026       $v_inclusion
  3674       $v_inclusion
  4027       = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
  3675       = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
  4028                                 $p_entry['filename']); 
  3676                                 $p_entry['filename']);
  4029       if ($v_inclusion == 0) {
  3677       if ($v_inclusion == 0) {
  4030         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_EXTRACT_DIR_RESTRICTION is selected, file is outside restriction");
       
  4031 
  3678 
  4032         PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
  3679         PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
  4033 			                     "Filename '".$p_entry['filename']."' is "
  3680 			                     "Filename '".$p_entry['filename']."' is "
  4034 								 ."outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
  3681 								 ."outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
  4035 
  3682 
  4036         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4037         return PclZip::errorCode();
  3683         return PclZip::errorCode();
  4038       }
  3684       }
  4039     }
  3685     }
  4040 
  3686 
  4041     // ----- Look for pre-extract callback
  3687     // ----- Look for pre-extract callback
  4042     if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  3688     if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  4043       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
       
  4044 
  3689 
  4045       // ----- Generate a local information
  3690       // ----- Generate a local information
  4046       $v_local_header = array();
  3691       $v_local_header = array();
  4047       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3692       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  4048 
  3693 
  4049       // ----- Call the callback
  3694       // ----- Call the callback
  4050       // Here I do not use call_user_func() because I need to send a reference to the
  3695       // Here I do not use call_user_func() because I need to send a reference to the
  4051       // header.
  3696       // header.
  4052       eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  3697       $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  4053       if ($v_result == 0) {
  3698       if ($v_result == 0) {
  4054         // ----- Change the file status
  3699         // ----- Change the file status
  4055         $p_entry['status'] = "skipped";
  3700         $p_entry['status'] = "skipped";
  4056         $v_result = 1;
  3701         $v_result = 1;
  4057       }
  3702       }
  4058       
  3703 
  4059       // ----- Look for abort result
  3704       // ----- Look for abort result
  4060       if ($v_result == 2) {
  3705       if ($v_result == 2) {
  4061         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
       
  4062         // ----- This status is internal and will be changed in 'skipped'
  3706         // ----- This status is internal and will be changed in 'skipped'
  4063         $p_entry['status'] = "aborted";
  3707         $p_entry['status'] = "aborted";
  4064       	$v_result = PCLZIP_ERR_USER_ABORTED;
  3708       	$v_result = PCLZIP_ERR_USER_ABORTED;
  4065       }
  3709       }
  4066 
  3710 
  4067       // ----- Update the informations
  3711       // ----- Update the informations
  4068       // Only some fields can be modified
  3712       // Only some fields can be modified
  4069       $p_entry['filename'] = $v_local_header['filename'];
  3713       $p_entry['filename'] = $v_local_header['filename'];
  4070       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
  3714     }
  4071     }
  3715 
  4072 
       
  4073     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
       
  4074 
  3716 
  4075     // ----- Look if extraction should be done
  3717     // ----- Look if extraction should be done
  4076     if ($p_entry['status'] == 'ok') {
  3718     if ($p_entry['status'] == 'ok') {
  4077 
  3719 
  4078     // ----- Look for specific actions while the file exist
  3720     // ----- Look for specific actions while the file exist
  4079     if (file_exists($p_entry['filename']))
  3721     if (file_exists($p_entry['filename']))
  4080     {
  3722     {
  4081       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$p_entry['filename']."' already exists");
       
  4082 
  3723 
  4083       // ----- Look if file is a directory
  3724       // ----- Look if file is a directory
  4084       if (is_dir($p_entry['filename']))
  3725       if (is_dir($p_entry['filename']))
  4085       {
  3726       {
  4086         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is a directory");
       
  4087 
  3727 
  4088         // ----- Change the file status
  3728         // ----- Change the file status
  4089         $p_entry['status'] = "already_a_directory";
  3729         $p_entry['status'] = "already_a_directory";
  4090         
  3730 
  4091         // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3731         // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  4092         // For historical reason first PclZip implementation does not stop
  3732         // For historical reason first PclZip implementation does not stop
  4093         // when this kind of error occurs.
  3733         // when this kind of error occurs.
  4094         if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3734         if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  4095 		    && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3735 		    && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  4096             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
       
  4097 
  3736 
  4098             PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
  3737             PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
  4099 			                     "Filename '".$p_entry['filename']."' is "
  3738 			                     "Filename '".$p_entry['filename']."' is "
  4100 								 ."already used by an existing directory");
  3739 								 ."already used by an existing directory");
  4101 
  3740 
  4102             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4103             return PclZip::errorCode();
  3741             return PclZip::errorCode();
  4104 		    }
  3742 		    }
  4105       }
  3743       }
  4106       // ----- Look if file is write protected
  3744       // ----- Look if file is write protected
  4107       else if (!is_writeable($p_entry['filename']))
  3745       else if (!is_writeable($p_entry['filename']))
  4108       {
  3746       {
  4109         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is write protected");
       
  4110 
  3747 
  4111         // ----- Change the file status
  3748         // ----- Change the file status
  4112         $p_entry['status'] = "write_protected";
  3749         $p_entry['status'] = "write_protected";
  4113 
  3750 
  4114         // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3751         // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  4115         // For historical reason first PclZip implementation does not stop
  3752         // For historical reason first PclZip implementation does not stop
  4116         // when this kind of error occurs.
  3753         // when this kind of error occurs.
  4117         if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3754         if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  4118 		    && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3755 		    && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  4119             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
       
  4120 
  3756 
  4121             PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  3757             PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  4122 			                     "Filename '".$p_entry['filename']."' exists "
  3758 			                     "Filename '".$p_entry['filename']."' exists "
  4123 								 ."and is write protected");
  3759 								 ."and is write protected");
  4124 
  3760 
  4125             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4126             return PclZip::errorCode();
  3761             return PclZip::errorCode();
  4127 		    }
  3762 		    }
  4128       }
  3763       }
  4129 
  3764 
  4130       // ----- Look if the extracted file is older
  3765       // ----- Look if the extracted file is older
  4131       else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
  3766       else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
  4132       {
  3767       {
  4133         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is newer (".date("l dS of F Y h:i:s A", filemtime($p_entry['filename'])).") than the extracted file (".date("l dS of F Y h:i:s A", $p_entry['mtime']).")");
       
  4134         // ----- Change the file status
  3768         // ----- Change the file status
  4135         if (   (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
  3769         if (   (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
  4136 		    && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
  3770 		    && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
  4137             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_REPLACE_NEWER is selected, file will be replaced");
       
  4138 	  	  }
  3771 	  	  }
  4139 		    else {
  3772 		    else {
  4140             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be replaced");
       
  4141             $p_entry['status'] = "newer_exist";
  3773             $p_entry['status'] = "newer_exist";
  4142 
  3774 
  4143             // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3775             // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  4144             // For historical reason first PclZip implementation does not stop
  3776             // For historical reason first PclZip implementation does not stop
  4145             // when this kind of error occurs.
  3777             // when this kind of error occurs.
  4146             if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3778             if (   (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  4147 		        && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3779 		        && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  4148                 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "PCLZIP_OPT_STOP_ON_ERROR is selected, extraction will be stopped");
       
  4149 
  3780 
  4150                 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  3781                 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  4151 			             "Newer version of '".$p_entry['filename']."' exists "
  3782 			             "Newer version of '".$p_entry['filename']."' exists "
  4152 					    ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
  3783 					    ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
  4153 
  3784 
  4154                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4155                 return PclZip::errorCode();
  3785                 return PclZip::errorCode();
  4156 		      }
  3786 		      }
  4157 		    }
  3787 		    }
  4158       }
  3788       }
  4159       else {
  3789       else {
  4160         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Existing file '".$p_entry['filename']."' is older than the extrated one - will be replaced by the extracted one (".date("l dS of F Y h:i:s A", filemtime($p_entry['filename'])).") than the extracted file (".date("l dS of F Y h:i:s A", $p_entry['mtime']).")");
       
  4161       }
  3790       }
  4162     }
  3791     }
  4163 
  3792 
  4164     // ----- Check the directory availability and create it if necessary
  3793     // ----- Check the directory availability and create it if necessary
  4165     else {
  3794     else {
  4169         $v_dir_to_check = "";
  3798         $v_dir_to_check = "";
  4170       else
  3799       else
  4171         $v_dir_to_check = dirname($p_entry['filename']);
  3800         $v_dir_to_check = dirname($p_entry['filename']);
  4172 
  3801 
  4173         if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
  3802         if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
  4174           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '".$p_entry['filename']."'");
  3803 
  4175   
       
  4176           // ----- Change the file status
  3804           // ----- Change the file status
  4177           $p_entry['status'] = "path_creation_fail";
  3805           $p_entry['status'] = "path_creation_fail";
  4178   
  3806 
  4179           // ----- Return
  3807           // ----- Return
  4180           ////--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4181           //return $v_result;
  3808           //return $v_result;
  4182           $v_result = 1;
  3809           $v_result = 1;
  4183         }
  3810         }
  4184       }
  3811       }
  4185     }
  3812     }
  4190       // ----- Do the extraction (if not a folder)
  3817       // ----- Do the extraction (if not a folder)
  4191       if (!(($p_entry['external']&0x00000010)==0x00000010))
  3818       if (!(($p_entry['external']&0x00000010)==0x00000010))
  4192       {
  3819       {
  4193         // ----- Look for not compressed file
  3820         // ----- Look for not compressed file
  4194         if ($p_entry['compression'] == 0) {
  3821         if ($p_entry['compression'] == 0) {
  4195           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
       
  4196 
  3822 
  4197     		  // ----- Opening destination file
  3823     		  // ----- Opening destination file
  4198           if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
  3824           if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
  4199           {
  3825           {
  4200             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
       
  4201 
  3826 
  4202             // ----- Change the file status
  3827             // ----- Change the file status
  4203             $p_entry['status'] = "write_error";
  3828             $p_entry['status'] = "write_error";
  4204 
  3829 
  4205             // ----- Return
  3830             // ----- Return
  4206             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4207             return $v_result;
  3831             return $v_result;
  4208           }
  3832           }
  4209 
  3833 
  4210           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read '".$p_entry['size']."' bytes");
       
  4211 
  3834 
  4212           // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3835           // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  4213           $v_size = $p_entry['compressed_size'];
  3836           $v_size = $p_entry['compressed_size'];
  4214           while ($v_size != 0)
  3837           while ($v_size != 0)
  4215           {
  3838           {
  4216             $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3839             $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4217             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read $v_read_size bytes");
       
  4218             $v_buffer = @fread($this->zip_fd, $v_read_size);
  3840             $v_buffer = @fread($this->zip_fd, $v_read_size);
  4219             /* Try to speed up the code
  3841             /* Try to speed up the code
  4220             $v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3842             $v_binary_data = pack('a'.$v_read_size, $v_buffer);
  4221             @fwrite($v_dest_file, $v_binary_data, $v_read_size);
  3843             @fwrite($v_dest_file, $v_binary_data, $v_read_size);
  4222             */
  3844             */
  4223             @fwrite($v_dest_file, $v_buffer, $v_read_size);            
  3845             @fwrite($v_dest_file, $v_buffer, $v_read_size);
  4224             $v_size -= $v_read_size;
  3846             $v_size -= $v_read_size;
  4225           }
  3847           }
  4226 
  3848 
  4227           // ----- Closing the destination file
  3849           // ----- Closing the destination file
  4228           fclose($v_dest_file);
  3850           fclose($v_dest_file);
  4229 
  3851 
  4230           // ----- Change the file mtime
  3852           // ----- Change the file mtime
  4231           touch($p_entry['filename'], $p_entry['mtime']);
  3853           touch($p_entry['filename'], $p_entry['mtime']);
  4232           
  3854 
  4233 
  3855 
  4234         }
  3856         }
  4235         else {
  3857         else {
  4236           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file (Compression method ".$p_entry['compression'].")");
       
  4237           // ----- TBC
  3858           // ----- TBC
  4238           // Need to be finished
  3859           // Need to be finished
  4239           if (($p_entry['flag'] & 1) == 1) {
  3860           if (($p_entry['flag'] & 1) == 1) {
  4240             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File is encrypted");
       
  4241             PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \''.$p_entry['filename'].'\' is encrypted. Encrypted files are not supported.');
  3861             PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \''.$p_entry['filename'].'\' is encrypted. Encrypted files are not supported.');
  4242             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4243             return PclZip::errorCode();
  3862             return PclZip::errorCode();
  4244           }
  3863           }
  4245 
  3864 
  4246 
  3865 
  4247           // ----- Look for using temporary file to unzip
  3866           // ----- Look for using temporary file to unzip
  4248           if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) 
  3867           if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  4249               && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  3868               && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  4250                   || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  3869                   || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  4251                       && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])) ) ) {
  3870                       && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])) ) ) {
  4252             $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
  3871             $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
  4253             if ($v_result < PCLZIP_ERR_NO_ERROR) {
  3872             if ($v_result < PCLZIP_ERR_NO_ERROR) {
  4254               return $v_result;
  3873               return $v_result;
  4255             }
  3874             }
  4256           }
  3875           }
  4257           
  3876 
  4258           // ----- Look for extract in memory
  3877           // ----- Look for extract in memory
  4259           else {
  3878           else {
  4260 
  3879 
  4261             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read '".$p_entry['compressed_size']."' compressed bytes");
  3880 
  4262           
       
  4263             // ----- Read the compressed file in a buffer (one shot)
  3881             // ----- Read the compressed file in a buffer (one shot)
  4264             $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3882             $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  4265             
  3883 
  4266             // ----- Decompress the file
  3884             // ----- Decompress the file
  4267             $v_file_content = @gzinflate($v_buffer);
  3885             $v_file_content = @gzinflate($v_buffer);
  4268             unset($v_buffer);
  3886             unset($v_buffer);
  4269             if ($v_file_content === FALSE) {
  3887             if ($v_file_content === FALSE) {
  4270               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to inflate compressed file");
  3888 
  4271   
       
  4272               // ----- Change the file status
  3889               // ----- Change the file status
  4273               // TBC
  3890               // TBC
  4274               $p_entry['status'] = "error";
  3891               $p_entry['status'] = "error";
  4275               
  3892 
  4276               //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4277               return $v_result;
  3893               return $v_result;
  4278             }
  3894             }
  4279             
  3895 
  4280             // ----- Opening destination file
  3896             // ----- Opening destination file
  4281             if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3897             if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  4282               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
  3898 
  4283   
       
  4284               // ----- Change the file status
  3899               // ----- Change the file status
  4285               $p_entry['status'] = "write_error";
  3900               $p_entry['status'] = "write_error";
  4286   
  3901 
  4287               //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4288               return $v_result;
  3902               return $v_result;
  4289             }
  3903             }
  4290   
  3904 
  4291             // ----- Write the uncompressed data
  3905             // ----- Write the uncompressed data
  4292             @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  3906             @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  4293             unset($v_file_content);
  3907             unset($v_file_content);
  4294   
  3908 
  4295             // ----- Closing the destination file
  3909             // ----- Closing the destination file
  4296             @fclose($v_dest_file);
  3910             @fclose($v_dest_file);
  4297             
  3911 
  4298           }
  3912           }
  4299 
  3913 
  4300           // ----- Change the file mtime
  3914           // ----- Change the file mtime
  4301           @touch($p_entry['filename'], $p_entry['mtime']);
  3915           @touch($p_entry['filename'], $p_entry['mtime']);
  4302         }
  3916         }
  4303 
  3917 
  4304         // ----- Look for chmod option
  3918         // ----- Look for chmod option
  4305         if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
  3919         if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
  4306           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "chmod option activated '".$p_options[PCLZIP_OPT_SET_CHMOD]."'");
       
  4307 
  3920 
  4308           // ----- Change the mode of the file
  3921           // ----- Change the mode of the file
  4309           @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
  3922           @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
  4310         }
  3923         }
  4311 
  3924 
  4312         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
  3925       }
  4313       }
  3926     }
  4314     }
  3927 
  4315 
  3928   	// ----- Change abort status
  4316 	// ----- Change abort status
  3929   	if ($p_entry['status'] == "aborted") {
  4317 	if ($p_entry['status'] == "aborted") {
  3930         $p_entry['status'] = "skipped";
  4318       $p_entry['status'] = "skipped";
  3931   	}
  4319 	}
  3932 
  4320 	
       
  4321     // ----- Look for post-extract callback
  3933     // ----- Look for post-extract callback
  4322     elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3934     elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  4323       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
       
  4324 
  3935 
  4325       // ----- Generate a local information
  3936       // ----- Generate a local information
  4326       $v_local_header = array();
  3937       $v_local_header = array();
  4327       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3938       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  4328 
  3939 
  4329       // ----- Call the callback
  3940       // ----- Call the callback
  4330       // Here I do not use call_user_func() because I need to send a reference to the
  3941       // Here I do not use call_user_func() because I need to send a reference to the
  4331       // header.
  3942       // header.
  4332       eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3943       $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  4333 
  3944 
  4334       // ----- Look for abort result
  3945       // ----- Look for abort result
  4335       if ($v_result == 2) {
  3946       if ($v_result == 2) {
  4336         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
       
  4337       	$v_result = PCLZIP_ERR_USER_ABORTED;
  3947       	$v_result = PCLZIP_ERR_USER_ABORTED;
  4338       }
  3948       }
  4339     }
  3949     }
  4340 
  3950 
  4341     // ----- Return
  3951     // ----- Return
  4342     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4343     return $v_result;
  3952     return $v_result;
  4344   }
  3953   }
  4345   // --------------------------------------------------------------------------------
  3954   // --------------------------------------------------------------------------------
  4346 
  3955 
  4347   // --------------------------------------------------------------------------------
  3956   // --------------------------------------------------------------------------------
  4350   // Parameters :
  3959   // Parameters :
  4351   // Return Values :
  3960   // Return Values :
  4352   // --------------------------------------------------------------------------------
  3961   // --------------------------------------------------------------------------------
  4353   function privExtractFileUsingTempFile(&$p_entry, &$p_options)
  3962   function privExtractFileUsingTempFile(&$p_entry, &$p_options)
  4354   {
  3963   {
  4355     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileUsingTempFile', "filename='".$p_entry['filename']."'");
       
  4356     $v_result=1;
  3964     $v_result=1;
  4357         
  3965 
  4358     // ----- Creates a temporary file
  3966     // ----- Creates a temporary file
  4359     $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  3967     $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  4360     if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
  3968     if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
  4361       fclose($v_file);
  3969       fclose($v_file);
  4362       PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  3970       PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  4363       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4364       return PclZip::errorCode();
  3971       return PclZip::errorCode();
  4365     }
  3972     }
  4366 
  3973 
  4367     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Start extraction of '".$p_entry['filename']."'");
       
  4368 
  3974 
  4369     // ----- Write gz file format header
  3975     // ----- Write gz file format header
  4370     $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
  3976     $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
  4371     @fwrite($v_dest_file, $v_binary_data, 10);
  3977     @fwrite($v_dest_file, $v_binary_data, 10);
  4372 
  3978 
  4373     // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3979     // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  4374     $v_size = $p_entry['compressed_size'];
  3980     $v_size = $p_entry['compressed_size'];
  4375     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Compressed Size :".$v_size."");
       
  4376     while ($v_size != 0)
  3981     while ($v_size != 0)
  4377     {
  3982     {
  4378       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3983       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4379       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read ".$v_read_size." bytes");
       
  4380       $v_buffer = @fread($this->zip_fd, $v_read_size);
  3984       $v_buffer = @fread($this->zip_fd, $v_read_size);
  4381       //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3985       //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  4382       @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3986       @fwrite($v_dest_file, $v_buffer, $v_read_size);
  4383       $v_size -= $v_read_size;
  3987       $v_size -= $v_read_size;
  4384     }
  3988     }
  4390     // ----- Close the temporary file
  3994     // ----- Close the temporary file
  4391     @fclose($v_dest_file);
  3995     @fclose($v_dest_file);
  4392 
  3996 
  4393     // ----- Opening destination file
  3997     // ----- Opening destination file
  4394     if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3998     if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  4395       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Error while opening '".$p_entry['filename']."' in write binary mode");
       
  4396       $p_entry['status'] = "write_error";
  3999       $p_entry['status'] = "write_error";
  4397       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4398       return $v_result;
  4000       return $v_result;
  4399     }
  4001     }
  4400 
  4002 
  4401     // ----- Open the temporary gz file
  4003     // ----- Open the temporary gz file
  4402     if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
  4004     if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
  4403       @fclose($v_dest_file);
  4005       @fclose($v_dest_file);
  4404       $p_entry['status'] = "read_error";
  4006       $p_entry['status'] = "read_error";
  4405       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  4007       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  4406       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4407       return PclZip::errorCode();
  4008       return PclZip::errorCode();
  4408     }
  4009     }
  4409 
  4010 
  4410     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'File size is '.filesize($v_gzip_temp_name));
       
  4411     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
       
  4412 
  4011 
  4413     // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  4012     // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  4414     $v_size = $p_entry['size'];
  4013     $v_size = $p_entry['size'];
  4415     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size :".$v_size."");
       
  4416     while ($v_size != 0) {
  4014     while ($v_size != 0) {
  4417       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4015       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4418       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Read ".$v_read_size." bytes");
       
  4419       $v_buffer = @gzread($v_src_file, $v_read_size);
  4016       $v_buffer = @gzread($v_src_file, $v_read_size);
  4420       //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  4017       //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  4421       @fwrite($v_dest_file, $v_buffer, $v_read_size);
  4018       @fwrite($v_dest_file, $v_buffer, $v_read_size);
  4422       $v_size -= $v_read_size;
  4019       $v_size -= $v_read_size;
  4423     }
  4020     }
  4424     @fclose($v_dest_file);
  4021     @fclose($v_dest_file);
  4425     @gzclose($v_src_file);
  4022     @gzclose($v_src_file);
  4426 
  4023 
  4427     // ----- Delete the temporary file
  4024     // ----- Delete the temporary file
  4428     @unlink($v_gzip_temp_name);
  4025     @unlink($v_gzip_temp_name);
  4429     
  4026 
  4430     // ----- Return
  4027     // ----- Return
  4431     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4432     return $v_result;
  4028     return $v_result;
  4433   }
  4029   }
  4434   // --------------------------------------------------------------------------------
  4030   // --------------------------------------------------------------------------------
  4435 
  4031 
  4436   // --------------------------------------------------------------------------------
  4032   // --------------------------------------------------------------------------------
  4439   // Parameters :
  4035   // Parameters :
  4440   // Return Values :
  4036   // Return Values :
  4441   // --------------------------------------------------------------------------------
  4037   // --------------------------------------------------------------------------------
  4442   function privExtractFileInOutput(&$p_entry, &$p_options)
  4038   function privExtractFileInOutput(&$p_entry, &$p_options)
  4443   {
  4039   {
  4444     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileInOutput', "");
       
  4445     $v_result=1;
  4040     $v_result=1;
  4446 
  4041 
  4447     // ----- Read the file header
  4042     // ----- Read the file header
  4448     if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  4043     if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  4449       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4450       return $v_result;
  4044       return $v_result;
  4451     }
  4045     }
  4452 
  4046 
  4453     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
       
  4454 
  4047 
  4455     // ----- Check that the file header is coherent with $p_entry info
  4048     // ----- Check that the file header is coherent with $p_entry info
  4456     if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  4049     if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  4457         // TBC
  4050         // TBC
  4458     }
  4051     }
  4459 
  4052 
  4460     // ----- Look for pre-extract callback
  4053     // ----- Look for pre-extract callback
  4461     if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  4054     if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  4462       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_EXTRACT]."()') is defined for the extraction");
       
  4463 
  4055 
  4464       // ----- Generate a local information
  4056       // ----- Generate a local information
  4465       $v_local_header = array();
  4057       $v_local_header = array();
  4466       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  4058       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  4467 
  4059 
  4468       // ----- Call the callback
  4060       // ----- Call the callback
  4469       // Here I do not use call_user_func() because I need to send a reference to the
  4061       // Here I do not use call_user_func() because I need to send a reference to the
  4470       // header.
  4062       // header.
  4471       eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  4063 //      eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
       
  4064       $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  4472       if ($v_result == 0) {
  4065       if ($v_result == 0) {
  4473         // ----- Change the file status
  4066         // ----- Change the file status
  4474         $p_entry['status'] = "skipped";
  4067         $p_entry['status'] = "skipped";
  4475         $v_result = 1;
  4068         $v_result = 1;
  4476       }
  4069       }
  4477 
  4070 
  4478       // ----- Look for abort result
  4071       // ----- Look for abort result
  4479       if ($v_result == 2) {
  4072       if ($v_result == 2) {
  4480         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
       
  4481         // ----- This status is internal and will be changed in 'skipped'
  4073         // ----- This status is internal and will be changed in 'skipped'
  4482         $p_entry['status'] = "aborted";
  4074         $p_entry['status'] = "aborted";
  4483       	$v_result = PCLZIP_ERR_USER_ABORTED;
  4075       	$v_result = PCLZIP_ERR_USER_ABORTED;
  4484       }
  4076       }
  4485 
  4077 
  4486       // ----- Update the informations
  4078       // ----- Update the informations
  4487       // Only some fields can be modified
  4079       // Only some fields can be modified
  4488       $p_entry['filename'] = $v_local_header['filename'];
  4080       $p_entry['filename'] = $v_local_header['filename'];
  4489       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New filename is '".$p_entry['filename']."'");
       
  4490     }
  4081     }
  4491 
  4082 
  4492     // ----- Trace
  4083     // ----- Trace
  4493     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '".$p_entry['filename']."', size '$v_header[size]'");
       
  4494 
  4084 
  4495     // ----- Look if extraction should be done
  4085     // ----- Look if extraction should be done
  4496     if ($p_entry['status'] == 'ok') {
  4086     if ($p_entry['status'] == 'ok') {
  4497 
  4087 
  4498       // ----- Do the extraction (if not a folder)
  4088       // ----- Do the extraction (if not a folder)
  4499       if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  4089       if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  4500         // ----- Look for not compressed file
  4090         // ----- Look for not compressed file
  4501         if ($p_entry['compressed_size'] == $p_entry['size']) {
  4091         if ($p_entry['compressed_size'] == $p_entry['size']) {
  4502           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
       
  4503           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
       
  4504 
  4092 
  4505           // ----- Read the file in a buffer (one shot)
  4093           // ----- Read the file in a buffer (one shot)
  4506           $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  4094           $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  4507 
  4095 
  4508           // ----- Send the file to the output
  4096           // ----- Send the file to the output
  4509           echo $v_buffer;
  4097           echo $v_buffer;
  4510           unset($v_buffer);
  4098           unset($v_buffer);
  4511         }
  4099         }
  4512         else {
  4100         else {
  4513           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file");
       
  4514           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Reading '".$p_entry['size']."' bytes");
       
  4515 
  4101 
  4516           // ----- Read the compressed file in a buffer (one shot)
  4102           // ----- Read the compressed file in a buffer (one shot)
  4517           $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  4103           $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  4518           
  4104 
  4519           // ----- Decompress the file
  4105           // ----- Decompress the file
  4520           $v_file_content = gzinflate($v_buffer);
  4106           $v_file_content = gzinflate($v_buffer);
  4521           unset($v_buffer);
  4107           unset($v_buffer);
  4522 
  4108 
  4523           // ----- Send the file to the output
  4109           // ----- Send the file to the output
  4524           echo $v_file_content;
  4110           echo $v_file_content;
  4525           unset($v_file_content);
  4111           unset($v_file_content);
  4526         }
  4112         }
  4527         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
       
  4528       }
  4113       }
  4529     }
  4114     }
  4530 
  4115 
  4531 	// ----- Change abort status
  4116 	// ----- Change abort status
  4532 	if ($p_entry['status'] == "aborted") {
  4117 	if ($p_entry['status'] == "aborted") {
  4533       $p_entry['status'] = "skipped";
  4118       $p_entry['status'] = "skipped";
  4534 	}
  4119 	}
  4535 
  4120 
  4536     // ----- Look for post-extract callback
  4121     // ----- Look for post-extract callback
  4537     elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  4122     elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  4538       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_EXTRACT]."()') is defined for the extraction");
       
  4539 
  4123 
  4540       // ----- Generate a local information
  4124       // ----- Generate a local information
  4541       $v_local_header = array();
  4125       $v_local_header = array();
  4542       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  4126       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  4543 
  4127 
  4544       // ----- Call the callback
  4128       // ----- Call the callback
  4545       // Here I do not use call_user_func() because I need to send a reference to the
  4129       // Here I do not use call_user_func() because I need to send a reference to the
  4546       // header.
  4130       // header.
  4547       eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  4131       $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  4548 
  4132 
  4549       // ----- Look for abort result
  4133       // ----- Look for abort result
  4550       if ($v_result == 2) {
  4134       if ($v_result == 2) {
  4551         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "User callback abort the extraction");
       
  4552       	$v_result = PCLZIP_ERR_USER_ABORTED;
  4135       	$v_result = PCLZIP_ERR_USER_ABORTED;
  4553       }
  4136       }
  4554     }
  4137     }
  4555 
  4138 
  4556     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4557     return $v_result;
  4139     return $v_result;
  4558   }
  4140   }
  4559   // --------------------------------------------------------------------------------
  4141   // --------------------------------------------------------------------------------
  4560 
  4142 
  4561   // --------------------------------------------------------------------------------
  4143   // --------------------------------------------------------------------------------
  4562   // Function : privExtractFileAsString()
  4144   // Function : privExtractFileAsString()
  4563   // Description :
  4145   // Description :
  4564   // Parameters :
  4146   // Parameters :
  4565   // Return Values :
  4147   // Return Values :
  4566   // --------------------------------------------------------------------------------
  4148   // --------------------------------------------------------------------------------
  4567   function privExtractFileAsString(&$p_entry, &$p_string)
  4149   function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
  4568   {
  4150   {
  4569     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privExtractFileAsString', "p_entry['filename']='".$p_entry['filename']."'");
       
  4570     $v_result=1;
  4151     $v_result=1;
  4571 
  4152 
  4572     // ----- Read the file header
  4153     // ----- Read the file header
  4573     $v_header = array();
  4154     $v_header = array();
  4574     if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  4155     if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  4575     {
  4156     {
  4576       // ----- Return
  4157       // ----- Return
  4577       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4578       return $v_result;
  4158       return $v_result;
  4579     }
  4159     }
  4580 
  4160 
  4581     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found file '".$v_header['filename']."', size '".$v_header['size']."'");
       
  4582 
  4161 
  4583     // ----- Check that the file header is coherent with $p_entry info
  4162     // ----- Check that the file header is coherent with $p_entry info
  4584     if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  4163     if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  4585         // TBC
  4164         // TBC
  4586     }
  4165     }
  4587 
  4166 
  4588     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting file in string (with path) '".$p_entry['filename']."', size '$v_header[size]'");
  4167     // ----- Look for pre-extract callback
  4589 
  4168     if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  4590     // ----- Do the extraction (if not a folder)
  4169 
  4591     if (!(($p_entry['external']&0x00000010)==0x00000010))
  4170       // ----- Generate a local information
  4592     {
  4171       $v_local_header = array();
  4593       // ----- Look for not compressed file
  4172       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  4594 //      if ($p_entry['compressed_size'] == $p_entry['size'])
  4173 
  4595       if ($p_entry['compression'] == 0) {
  4174       // ----- Call the callback
  4596         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting an un-compressed file");
  4175       // Here I do not use call_user_func() because I need to send a reference to the
  4597         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Reading '".$p_entry['size']."' bytes");
  4176       // header.
  4598 
  4177       $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  4599         // ----- Reading the file
  4178       if ($v_result == 0) {
  4600         $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
  4179         // ----- Change the file status
       
  4180         $p_entry['status'] = "skipped";
       
  4181         $v_result = 1;
       
  4182       }
       
  4183 
       
  4184       // ----- Look for abort result
       
  4185       if ($v_result == 2) {
       
  4186         // ----- This status is internal and will be changed in 'skipped'
       
  4187         $p_entry['status'] = "aborted";
       
  4188       	$v_result = PCLZIP_ERR_USER_ABORTED;
       
  4189       }
       
  4190 
       
  4191       // ----- Update the informations
       
  4192       // Only some fields can be modified
       
  4193       $p_entry['filename'] = $v_local_header['filename'];
       
  4194     }
       
  4195 
       
  4196 
       
  4197     // ----- Look if extraction should be done
       
  4198     if ($p_entry['status'] == 'ok') {
       
  4199 
       
  4200       // ----- Do the extraction (if not a folder)
       
  4201       if (!(($p_entry['external']&0x00000010)==0x00000010)) {
       
  4202         // ----- Look for not compressed file
       
  4203   //      if ($p_entry['compressed_size'] == $p_entry['size'])
       
  4204         if ($p_entry['compression'] == 0) {
       
  4205 
       
  4206           // ----- Reading the file
       
  4207           $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
       
  4208         }
       
  4209         else {
       
  4210 
       
  4211           // ----- Reading the file
       
  4212           $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
       
  4213 
       
  4214           // ----- Decompress the file
       
  4215           if (($p_string = @gzinflate($v_data)) === FALSE) {
       
  4216               // TBC
       
  4217           }
       
  4218         }
       
  4219 
       
  4220         // ----- Trace
  4601       }
  4221       }
  4602       else {
  4222       else {
  4603         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extracting a compressed file (compression method '".$p_entry['compression']."')");
  4223           // TBC : error : can not extract a folder in a string
  4604 
  4224       }
  4605         // ----- Reading the file
  4225 
  4606         $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
  4226     }
  4607         
  4227 
  4608         // ----- Decompress the file
  4228   	// ----- Change abort status
  4609         if (($p_string = @gzinflate($v_data)) === FALSE) {
  4229   	if ($p_entry['status'] == "aborted") {
  4610             // TBC
  4230         $p_entry['status'] = "skipped";
  4611         }
  4231   	}
  4612       }
  4232 
  4613 
  4233     // ----- Look for post-extract callback
  4614       // ----- Trace
  4234     elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  4615       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Extraction done");
  4235 
  4616     }
  4236       // ----- Generate a local information
  4617     else {
  4237       $v_local_header = array();
  4618         // TBC : error : can not extract a folder in a string
  4238       $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
       
  4239 
       
  4240       // ----- Swap the content to header
       
  4241       $v_local_header['content'] = $p_string;
       
  4242       $p_string = '';
       
  4243 
       
  4244       // ----- Call the callback
       
  4245       // Here I do not use call_user_func() because I need to send a reference to the
       
  4246       // header.
       
  4247       $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
       
  4248 
       
  4249       // ----- Swap back the content to header
       
  4250       $p_string = $v_local_header['content'];
       
  4251       unset($v_local_header['content']);
       
  4252 
       
  4253       // ----- Look for abort result
       
  4254       if ($v_result == 2) {
       
  4255       	$v_result = PCLZIP_ERR_USER_ABORTED;
       
  4256       }
  4619     }
  4257     }
  4620 
  4258 
  4621     // ----- Return
  4259     // ----- Return
  4622     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4623     return $v_result;
  4260     return $v_result;
  4624   }
  4261   }
  4625   // --------------------------------------------------------------------------------
  4262   // --------------------------------------------------------------------------------
  4626 
  4263 
  4627   // --------------------------------------------------------------------------------
  4264   // --------------------------------------------------------------------------------
  4630   // Parameters :
  4267   // Parameters :
  4631   // Return Values :
  4268   // Return Values :
  4632   // --------------------------------------------------------------------------------
  4269   // --------------------------------------------------------------------------------
  4633   function privReadFileHeader(&$p_header)
  4270   function privReadFileHeader(&$p_header)
  4634   {
  4271   {
  4635     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadFileHeader", "");
       
  4636     $v_result=1;
  4272     $v_result=1;
  4637 
  4273 
  4638     // ----- Read the 4 bytes signature
  4274     // ----- Read the 4 bytes signature
  4639     $v_binary_data = @fread($this->zip_fd, 4);
  4275     $v_binary_data = @fread($this->zip_fd, 4);
  4640     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
       
  4641     $v_data = unpack('Vid', $v_binary_data);
  4276     $v_data = unpack('Vid', $v_binary_data);
  4642     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
       
  4643 
  4277 
  4644     // ----- Check signature
  4278     // ----- Check signature
  4645     if ($v_data['id'] != 0x04034b50)
  4279     if ($v_data['id'] != 0x04034b50)
  4646     {
  4280     {
  4647       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid File header");
       
  4648 
  4281 
  4649       // ----- Error log
  4282       // ----- Error log
  4650       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  4283       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  4651 
  4284 
  4652       // ----- Return
  4285       // ----- Return
  4653       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4654       return PclZip::errorCode();
  4286       return PclZip::errorCode();
  4655     }
  4287     }
  4656 
  4288 
  4657     // ----- Read the first 42 bytes of the header
  4289     // ----- Read the first 42 bytes of the header
  4658     $v_binary_data = fread($this->zip_fd, 26);
  4290     $v_binary_data = fread($this->zip_fd, 26);
  4660     // ----- Look for invalid block size
  4292     // ----- Look for invalid block size
  4661     if (strlen($v_binary_data) != 26)
  4293     if (strlen($v_binary_data) != 26)
  4662     {
  4294     {
  4663       $p_header['filename'] = "";
  4295       $p_header['filename'] = "";
  4664       $p_header['status'] = "invalid_header";
  4296       $p_header['status'] = "invalid_header";
  4665       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
       
  4666 
  4297 
  4667       // ----- Error log
  4298       // ----- Error log
  4668       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  4299       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  4669 
  4300 
  4670       // ----- Return
  4301       // ----- Return
  4671       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4672       return PclZip::errorCode();
  4302       return PclZip::errorCode();
  4673     }
  4303     }
  4674 
  4304 
  4675     // ----- Extract the values
  4305     // ----- Extract the values
  4676     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header : '".$v_binary_data."'");
       
  4677     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header (Hex) : '".bin2hex($v_binary_data)."'");
       
  4678     $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  4306     $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  4679 
  4307 
  4680     // ----- Get filename
  4308     // ----- Get filename
  4681     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "File name length : ".$v_data['filename_len']);
       
  4682     $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
  4309     $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
  4683     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename : \''.$p_header['filename'].'\'');
       
  4684 
  4310 
  4685     // ----- Get extra_fields
  4311     // ----- Get extra_fields
  4686     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extra field length : ".$v_data['extra_len']);
       
  4687     if ($v_data['extra_len'] != 0) {
  4312     if ($v_data['extra_len'] != 0) {
  4688       $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  4313       $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  4689     }
  4314     }
  4690     else {
  4315     else {
  4691       $p_header['extra'] = '';
  4316       $p_header['extra'] = '';
  4692     }
  4317     }
  4693     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Extra field : \''.bin2hex($p_header['extra']).'\'');
       
  4694 
  4318 
  4695     // ----- Extract properties
  4319     // ----- Extract properties
  4696     $p_header['version_extracted'] = $v_data['version'];
  4320     $p_header['version_extracted'] = $v_data['version'];
  4697     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : ('.$p_header['version_extracted'].') \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
       
  4698     $p_header['compression'] = $v_data['compression'];
  4321     $p_header['compression'] = $v_data['compression'];
  4699     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compression method : \''.$p_header['compression'].'\'');
       
  4700     $p_header['size'] = $v_data['size'];
  4322     $p_header['size'] = $v_data['size'];
  4701     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_header['size'].'\'');
       
  4702     $p_header['compressed_size'] = $v_data['compressed_size'];
  4323     $p_header['compressed_size'] = $v_data['compressed_size'];
  4703     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
       
  4704     $p_header['crc'] = $v_data['crc'];
  4324     $p_header['crc'] = $v_data['crc'];
  4705     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
       
  4706     $p_header['flag'] = $v_data['flag'];
  4325     $p_header['flag'] = $v_data['flag'];
  4707     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag : \''.$p_header['flag'].'\'');
       
  4708     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag bit 11 (from right) : \''.($p_header['flag']&0x0400).'\'');
       
  4709     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag bit 11 (from left) : \''.($p_header['flag']&0x0020).'\'');
       
  4710     $p_header['filename_len'] = $v_data['filename_len'];
  4326     $p_header['filename_len'] = $v_data['filename_len'];
  4711     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename_len : \''.$p_header['filename_len'].'\'');
       
  4712 
  4327 
  4713     // ----- Recuperate date in UNIX format
  4328     // ----- Recuperate date in UNIX format
  4714     $p_header['mdate'] = $v_data['mdate'];
  4329     $p_header['mdate'] = $v_data['mdate'];
  4715     $p_header['mtime'] = $v_data['mtime'];
  4330     $p_header['mtime'] = $v_data['mtime'];
  4716     if ($p_header['mdate'] && $p_header['mtime'])
  4331     if ($p_header['mdate'] && $p_header['mtime'])
  4726       $v_day = $p_header['mdate'] & 0x001F;
  4341       $v_day = $p_header['mdate'] & 0x001F;
  4727 
  4342 
  4728       // ----- Get UNIX date format
  4343       // ----- Get UNIX date format
  4729       $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  4344       $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  4730 
  4345 
  4731       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
       
  4732     }
  4346     }
  4733     else
  4347     else
  4734     {
  4348     {
  4735       $p_header['mtime'] = time();
  4349       $p_header['mtime'] = time();
  4736       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
       
  4737     }
  4350     }
  4738 
  4351 
  4739     // TBC
  4352     // TBC
  4740     //for(reset($v_data); $key = key($v_data); next($v_data)) {
  4353     //for(reset($v_data); $key = key($v_data); next($v_data)) {
  4741     //  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Attribut[$key] = ".$v_data[$key]);
       
  4742     //}
  4354     //}
  4743 
  4355 
  4744     // ----- Set the stored filename
  4356     // ----- Set the stored filename
  4745     $p_header['stored_filename'] = $p_header['filename'];
  4357     $p_header['stored_filename'] = $p_header['filename'];
  4746 
  4358 
  4747     // ----- Set the status field
  4359     // ----- Set the status field
  4748     $p_header['status'] = "ok";
  4360     $p_header['status'] = "ok";
  4749 
  4361 
  4750     // ----- Return
  4362     // ----- Return
  4751     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4752     return $v_result;
  4363     return $v_result;
  4753   }
  4364   }
  4754   // --------------------------------------------------------------------------------
  4365   // --------------------------------------------------------------------------------
  4755 
  4366 
  4756   // --------------------------------------------------------------------------------
  4367   // --------------------------------------------------------------------------------
  4759   // Parameters :
  4370   // Parameters :
  4760   // Return Values :
  4371   // Return Values :
  4761   // --------------------------------------------------------------------------------
  4372   // --------------------------------------------------------------------------------
  4762   function privReadCentralFileHeader(&$p_header)
  4373   function privReadCentralFileHeader(&$p_header)
  4763   {
  4374   {
  4764     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadCentralFileHeader", "");
       
  4765     $v_result=1;
  4375     $v_result=1;
  4766 
  4376 
  4767     // ----- Read the 4 bytes signature
  4377     // ----- Read the 4 bytes signature
  4768     $v_binary_data = @fread($this->zip_fd, 4);
  4378     $v_binary_data = @fread($this->zip_fd, 4);
  4769     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
       
  4770     $v_data = unpack('Vid', $v_binary_data);
  4379     $v_data = unpack('Vid', $v_binary_data);
  4771     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
       
  4772 
  4380 
  4773     // ----- Check signature
  4381     // ----- Check signature
  4774     if ($v_data['id'] != 0x02014b50)
  4382     if ($v_data['id'] != 0x02014b50)
  4775     {
  4383     {
  4776       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid Central Dir File signature");
       
  4777 
  4384 
  4778       // ----- Error log
  4385       // ----- Error log
  4779       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  4386       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  4780 
  4387 
  4781       // ----- Return
  4388       // ----- Return
  4782       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4783       return PclZip::errorCode();
  4389       return PclZip::errorCode();
  4784     }
  4390     }
  4785 
  4391 
  4786     // ----- Read the first 42 bytes of the header
  4392     // ----- Read the first 42 bytes of the header
  4787     $v_binary_data = fread($this->zip_fd, 42);
  4393     $v_binary_data = fread($this->zip_fd, 42);
  4789     // ----- Look for invalid block size
  4395     // ----- Look for invalid block size
  4790     if (strlen($v_binary_data) != 42)
  4396     if (strlen($v_binary_data) != 42)
  4791     {
  4397     {
  4792       $p_header['filename'] = "";
  4398       $p_header['filename'] = "";
  4793       $p_header['status'] = "invalid_header";
  4399       $p_header['status'] = "invalid_header";
  4794       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));
       
  4795 
  4400 
  4796       // ----- Error log
  4401       // ----- Error log
  4797       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  4402       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  4798 
  4403 
  4799       // ----- Return
  4404       // ----- Return
  4800       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4801       return PclZip::errorCode();
  4405       return PclZip::errorCode();
  4802     }
  4406     }
  4803 
  4407 
  4804     // ----- Extract the values
  4408     // ----- Extract the values
  4805     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header : '".$v_binary_data."'");
       
  4806     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header (Hex) : '".bin2hex($v_binary_data)."'");
       
  4807     $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
  4409     $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);
  4808 
  4410 
  4809     // ----- Get filename
  4411     // ----- Get filename
  4810     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File name length : ".$p_header['filename_len']);
       
  4811     if ($p_header['filename_len'] != 0)
  4412     if ($p_header['filename_len'] != 0)
  4812       $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
  4413       $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
  4813     else
  4414     else
  4814       $p_header['filename'] = '';
  4415       $p_header['filename'] = '';
  4815     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Filename : \''.$p_header['filename'].'\'');
       
  4816 
  4416 
  4817     // ----- Get extra
  4417     // ----- Get extra
  4818     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Extra length : ".$p_header['extra_len']);
       
  4819     if ($p_header['extra_len'] != 0)
  4418     if ($p_header['extra_len'] != 0)
  4820       $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
  4419       $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
  4821     else
  4420     else
  4822       $p_header['extra'] = '';
  4421       $p_header['extra'] = '';
  4823     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Extra : \''.$p_header['extra'].'\'');
       
  4824 
  4422 
  4825     // ----- Get comment
  4423     // ----- Get comment
  4826     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Comment length : ".$p_header['comment_len']);
       
  4827     if ($p_header['comment_len'] != 0)
  4424     if ($p_header['comment_len'] != 0)
  4828       $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
  4425       $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
  4829     else
  4426     else
  4830       $p_header['comment'] = '';
  4427       $p_header['comment'] = '';
  4831     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Comment : \''.$p_header['comment'].'\'');
       
  4832 
  4428 
  4833     // ----- Extract properties
  4429     // ----- Extract properties
  4834     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version : \''.($p_header['version']/10).'.'.($p_header['version']%10).'\'');
       
  4835     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
       
  4836     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Size : \''.$p_header['size'].'\'');
       
  4837     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
       
  4838     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
       
  4839     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Flag : \''.$p_header['flag'].'\'');
       
  4840     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Offset : \''.$p_header['offset'].'\'');
       
  4841 
  4430 
  4842     // ----- Recuperate date in UNIX format
  4431     // ----- Recuperate date in UNIX format
  4843     //if ($p_header['mdate'] && $p_header['mtime'])
  4432     //if ($p_header['mdate'] && $p_header['mtime'])
  4844     // TBC : bug : this was ignoring time with 0/0/0
  4433     // TBC : bug : this was ignoring time with 0/0/0
  4845     if (1)
  4434     if (1)
  4855       $v_day = $p_header['mdate'] & 0x001F;
  4444       $v_day = $p_header['mdate'] & 0x001F;
  4856 
  4445 
  4857       // ----- Get UNIX date format
  4446       // ----- Get UNIX date format
  4858       $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  4447       $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  4859 
  4448 
  4860       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
       
  4861     }
  4449     }
  4862     else
  4450     else
  4863     {
  4451     {
  4864       $p_header['mtime'] = time();
  4452       $p_header['mtime'] = time();
  4865       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
       
  4866     }
  4453     }
  4867 
  4454 
  4868     // ----- Set the stored filename
  4455     // ----- Set the stored filename
  4869     $p_header['stored_filename'] = $p_header['filename'];
  4456     $p_header['stored_filename'] = $p_header['filename'];
  4870 
  4457 
  4871     // ----- Set default status to ok
  4458     // ----- Set default status to ok
  4872     $p_header['status'] = 'ok';
  4459     $p_header['status'] = 'ok';
  4873 
  4460 
  4874     // ----- Look if it is a directory
  4461     // ----- Look if it is a directory
  4875     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Internal (Hex) : '".sprintf("Ox%04X", $p_header['internal'])."'");
       
  4876     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "External (Hex) : '".sprintf("Ox%04X", $p_header['external'])."' (".(($p_header['external']&0x00000010)==0x00000010?'is a folder':'is a file').')');
       
  4877     if (substr($p_header['filename'], -1) == '/') {
  4462     if (substr($p_header['filename'], -1) == '/') {
  4878       //$p_header['external'] = 0x41FF0010;
  4463       //$p_header['external'] = 0x41FF0010;
  4879       $p_header['external'] = 0x00000010;
  4464       $p_header['external'] = 0x00000010;
  4880       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Force folder external : \''.sprintf("Ox%04X", $p_header['external']).'\'');
  4465     }
  4881     }
  4466 
  4882 
       
  4883     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Header of filename : \''.$p_header['filename'].'\'');
       
  4884 
  4467 
  4885     // ----- Return
  4468     // ----- Return
  4886     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4887     return $v_result;
  4469     return $v_result;
  4888   }
  4470   }
  4889   // --------------------------------------------------------------------------------
  4471   // --------------------------------------------------------------------------------
  4890 
  4472 
  4891   // --------------------------------------------------------------------------------
  4473   // --------------------------------------------------------------------------------
  4896   //   1 on success,
  4478   //   1 on success,
  4897   //   0 on error;
  4479   //   0 on error;
  4898   // --------------------------------------------------------------------------------
  4480   // --------------------------------------------------------------------------------
  4899   function privCheckFileHeaders(&$p_local_header, &$p_central_header)
  4481   function privCheckFileHeaders(&$p_local_header, &$p_central_header)
  4900   {
  4482   {
  4901     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFileHeaders", "");
       
  4902     $v_result=1;
  4483     $v_result=1;
  4903 
  4484 
  4904   	// ----- Check the static values
  4485   	// ----- Check the static values
  4905   	// TBC
  4486   	// TBC
  4906   	if ($p_local_header['filename'] != $p_central_header['filename']) {
  4487   	if ($p_local_header['filename'] != $p_central_header['filename']) {
  4907           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename" : TBC To Be Completed');
       
  4908   	}
  4488   	}
  4909   	if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
  4489   	if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
  4910           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "version_extracted" : TBC To Be Completed');
       
  4911   	}
  4490   	}
  4912   	if ($p_local_header['flag'] != $p_central_header['flag']) {
  4491   	if ($p_local_header['flag'] != $p_central_header['flag']) {
  4913           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "flag" : TBC To Be Completed');
       
  4914   	}
  4492   	}
  4915   	if ($p_local_header['compression'] != $p_central_header['compression']) {
  4493   	if ($p_local_header['compression'] != $p_central_header['compression']) {
  4916           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "compression" : TBC To Be Completed');
       
  4917   	}
  4494   	}
  4918   	if ($p_local_header['mtime'] != $p_central_header['mtime']) {
  4495   	if ($p_local_header['mtime'] != $p_central_header['mtime']) {
  4919           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "mtime" : TBC To Be Completed');
       
  4920   	}
  4496   	}
  4921   	if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
  4497   	if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
  4922           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Bad check "filename_len" : TBC To Be Completed');
       
  4923   	}
  4498   	}
  4924   
  4499 
  4925   	// ----- Look for flag bit 3
  4500   	// ----- Look for flag bit 3
  4926   	if (($p_local_header['flag'] & 8) == 8) {
  4501   	if (($p_local_header['flag'] & 8) == 8) {
  4927           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Purpose bit flag bit 3 set !');
       
  4928           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'File size, compression size and crc found in central header');
       
  4929           $p_local_header['size'] = $p_central_header['size'];
  4502           $p_local_header['size'] = $p_central_header['size'];
  4930           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_local_header['size'].'\'');
       
  4931           $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
  4503           $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
  4932           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_local_header['compressed_size'].'\'');
       
  4933           $p_local_header['crc'] = $p_central_header['crc'];
  4504           $p_local_header['crc'] = $p_central_header['crc'];
  4934           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_local_header['crc']).'\'');
       
  4935   	}
  4505   	}
  4936 
  4506 
  4937     // ----- Return
  4507     // ----- Return
  4938     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  4939     return $v_result;
  4508     return $v_result;
  4940   }
  4509   }
  4941   // --------------------------------------------------------------------------------
  4510   // --------------------------------------------------------------------------------
  4942 
  4511 
  4943   // --------------------------------------------------------------------------------
  4512   // --------------------------------------------------------------------------------
  4946   // Parameters :
  4515   // Parameters :
  4947   // Return Values :
  4516   // Return Values :
  4948   // --------------------------------------------------------------------------------
  4517   // --------------------------------------------------------------------------------
  4949   function privReadEndCentralDir(&$p_central_dir)
  4518   function privReadEndCentralDir(&$p_central_dir)
  4950   {
  4519   {
  4951     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadEndCentralDir", "");
       
  4952     $v_result=1;
  4520     $v_result=1;
  4953 
  4521 
  4954     // ----- Go to the end of the zip file
  4522     // ----- Go to the end of the zip file
  4955     $v_size = filesize($this->zipname);
  4523     $v_size = filesize($this->zipname);
  4956     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size of the file :$v_size");
       
  4957     @fseek($this->zip_fd, $v_size);
  4524     @fseek($this->zip_fd, $v_size);
  4958     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position at end of zip file : \''.ftell($this->zip_fd).'\'');
       
  4959     if (@ftell($this->zip_fd) != $v_size)
  4525     if (@ftell($this->zip_fd) != $v_size)
  4960     {
  4526     {
  4961       // ----- Error log
  4527       // ----- Error log
  4962       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
  4528       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
  4963 
  4529 
  4964       // ----- Return
  4530       // ----- Return
  4965       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4966       return PclZip::errorCode();
  4531       return PclZip::errorCode();
  4967     }
  4532     }
  4968 
  4533 
  4969     // ----- First try : look if this is an archive with no commentaries (most of the time)
  4534     // ----- First try : look if this is an archive with no commentaries (most of the time)
  4970     // in this case the end of central dir is at 22 bytes of the file end
  4535     // in this case the end of central dir is at 22 bytes of the file end
  4971     $v_found = 0;
  4536     $v_found = 0;
  4972     if ($v_size > 26) {
  4537     if ($v_size > 26) {
  4973       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Look for central dir with no comment');
       
  4974       @fseek($this->zip_fd, $v_size-22);
  4538       @fseek($this->zip_fd, $v_size-22);
  4975       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after min central position : \''.ftell($this->zip_fd).'\'');
       
  4976       if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
  4539       if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
  4977       {
  4540       {
  4978         // ----- Error log
  4541         // ----- Error log
  4979         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  4542         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  4980 
  4543 
  4981         // ----- Return
  4544         // ----- Return
  4982         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  4983         return PclZip::errorCode();
  4545         return PclZip::errorCode();
  4984       }
  4546       }
  4985 
  4547 
  4986       // ----- Read for bytes
  4548       // ----- Read for bytes
  4987       $v_binary_data = @fread($this->zip_fd, 4);
  4549       $v_binary_data = @fread($this->zip_fd, 4);
  4988       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
       
  4989       $v_data = @unpack('Vid', $v_binary_data);
  4550       $v_data = @unpack('Vid', $v_binary_data);
  4990       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
       
  4991 
  4551 
  4992       // ----- Check signature
  4552       // ----- Check signature
  4993       if ($v_data['id'] == 0x06054b50) {
  4553       if ($v_data['id'] == 0x06054b50) {
  4994         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found central dir at the default position.");
       
  4995         $v_found = 1;
  4554         $v_found = 1;
  4996       }
  4555       }
  4997 
  4556 
  4998       $v_pos = ftell($this->zip_fd);
  4557       $v_pos = ftell($this->zip_fd);
  4999     }
  4558     }
  5000 
  4559 
  5001     // ----- Go back to the maximum possible size of the Central Dir End Record
  4560     // ----- Go back to the maximum possible size of the Central Dir End Record
  5002     if (!$v_found) {
  4561     if (!$v_found) {
  5003       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Start extended search of end central dir');
       
  5004       $v_maximum_size = 65557; // 0xFFFF + 22;
  4562       $v_maximum_size = 65557; // 0xFFFF + 22;
  5005       if ($v_maximum_size > $v_size)
  4563       if ($v_maximum_size > $v_size)
  5006         $v_maximum_size = $v_size;
  4564         $v_maximum_size = $v_size;
  5007       @fseek($this->zip_fd, $v_size-$v_maximum_size);
  4565       @fseek($this->zip_fd, $v_size-$v_maximum_size);
  5008       if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
  4566       if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
  5009       {
  4567       {
  5010         // ----- Error log
  4568         // ----- Error log
  5011         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  4569         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  5012 
  4570 
  5013         // ----- Return
  4571         // ----- Return
  5014         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  5015         return PclZip::errorCode();
  4572         return PclZip::errorCode();
  5016       }
  4573       }
  5017       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after max central position : \''.ftell($this->zip_fd).'\'');
       
  5018 
  4574 
  5019       // ----- Read byte per byte in order to find the signature
  4575       // ----- Read byte per byte in order to find the signature
  5020       $v_pos = ftell($this->zip_fd);
  4576       $v_pos = ftell($this->zip_fd);
  5021       $v_bytes = 0x00000000;
  4577       $v_bytes = 0x00000000;
  5022       while ($v_pos < $v_size)
  4578       while ($v_pos < $v_size)
  5023       {
  4579       {
  5024         // ----- Read a byte
  4580         // ----- Read a byte
  5025         $v_byte = @fread($this->zip_fd, 1);
  4581         $v_byte = @fread($this->zip_fd, 1);
  5026 
  4582 
  5027         // -----  Add the byte
  4583         // -----  Add the byte
  5028         // $v_bytes = ($v_bytes << 8) | Ord($v_byte);
  4584         //$v_bytes = ($v_bytes << 8) | Ord($v_byte);
  5029         // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number 
  4585         // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
  5030         // Otherwise on systems where we have 64bit integers the check below for the magic number will fail. 
  4586         // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
  5031         $v_bytes = ( ($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte); 
  4587         $v_bytes = ( ($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
  5032 
  4588 
  5033         // ----- Compare the bytes
  4589         // ----- Compare the bytes
  5034         if ($v_bytes == 0x504b0506)
  4590         if ($v_bytes == 0x504b0506)
  5035         {
  4591         {
  5036           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Found End Central Dir signature at position : \''.ftell($this->zip_fd).'\'');
       
  5037           $v_pos++;
  4592           $v_pos++;
  5038           break;
  4593           break;
  5039         }
  4594         }
  5040 
  4595 
  5041         $v_pos++;
  4596         $v_pos++;
  5042       }
  4597       }
  5043 
  4598 
  5044       // ----- Look if not found end of central dir
  4599       // ----- Look if not found end of central dir
  5045       if ($v_pos == $v_size)
  4600       if ($v_pos == $v_size)
  5046       {
  4601       {
  5047         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to find End of Central Dir Record signature");
       
  5048 
  4602 
  5049         // ----- Error log
  4603         // ----- Error log
  5050         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
  4604         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
  5051 
  4605 
  5052         // ----- Return
  4606         // ----- Return
  5053         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  5054         return PclZip::errorCode();
  4607         return PclZip::errorCode();
  5055       }
  4608       }
  5056     }
  4609     }
  5057 
  4610 
  5058     // ----- Read the first 18 bytes of the header
  4611     // ----- Read the first 18 bytes of the header
  5059     $v_binary_data = fread($this->zip_fd, 18);
  4612     $v_binary_data = fread($this->zip_fd, 18);
  5060 
  4613 
  5061     // ----- Look for invalid block size
  4614     // ----- Look for invalid block size
  5062     if (strlen($v_binary_data) != 18)
  4615     if (strlen($v_binary_data) != 18)
  5063     {
  4616     {
  5064       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
       
  5065 
  4617 
  5066       // ----- Error log
  4618       // ----- Error log
  5067       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
  4619       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
  5068 
  4620 
  5069       // ----- Return
  4621       // ----- Return
  5070       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  5071       return PclZip::errorCode();
  4622       return PclZip::errorCode();
  5072     }
  4623     }
  5073 
  4624 
  5074     // ----- Extract the values
  4625     // ----- Extract the values
  5075     ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record : '".$v_binary_data."'");
       
  5076     ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record (Hex) : '".bin2hex($v_binary_data)."'");
       
  5077     $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  4626     $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  5078 
  4627 
  5079     // ----- Check the global size
  4628     // ----- Check the global size
  5080     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Comment length : ".$v_data['comment_size']);
       
  5081     if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  4629     if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  5082       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The central dir is not at the end of the archive. Some trailing bytes exists after the archive.");
       
  5083 
  4630 
  5084 	  // ----- Removed in release 2.2 see readme file
  4631 	  // ----- Removed in release 2.2 see readme file
  5085 	  // The check of the file size is a little too strict.
  4632 	  // The check of the file size is a little too strict.
  5086 	  // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
  4633 	  // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
  5087 	  // While decrypted, zip has training 0 bytes
  4634 	  // While decrypted, zip has training 0 bytes
  5090       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
  4637       PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
  5091 	                       'The central dir is not at the end of the archive.'
  4638 	                       'The central dir is not at the end of the archive.'
  5092 						   .' Some trailing bytes exists after the archive.');
  4639 						   .' Some trailing bytes exists after the archive.');
  5093 
  4640 
  5094       // ----- Return
  4641       // ----- Return
  5095       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  5096       return PclZip::errorCode();
  4642       return PclZip::errorCode();
  5097 	  }
  4643 	  }
  5098     }
  4644     }
  5099 
  4645 
  5100     // ----- Get comment
  4646     // ----- Get comment
  5101     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment size : \''.$v_data['comment_size'].'\'');
       
  5102     if ($v_data['comment_size'] != 0) {
  4647     if ($v_data['comment_size'] != 0) {
  5103       $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
  4648       $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
  5104     }
  4649     }
  5105     else
  4650     else
  5106       $p_central_dir['comment'] = '';
  4651       $p_central_dir['comment'] = '';
  5107     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment : \''.$p_central_dir['comment'].'\'');
       
  5108 
  4652 
  5109     $p_central_dir['entries'] = $v_data['entries'];
  4653     $p_central_dir['entries'] = $v_data['entries'];
  5110     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries : \''.$p_central_dir['entries'].'\'');
       
  5111     $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  4654     $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  5112     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries for this disk : \''.$p_central_dir['disk_entries'].'\'');
       
  5113     $p_central_dir['offset'] = $v_data['offset'];
  4655     $p_central_dir['offset'] = $v_data['offset'];
  5114     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Offset of Central Dir : \''.$p_central_dir['offset'].'\'');
       
  5115     $p_central_dir['size'] = $v_data['size'];
  4656     $p_central_dir['size'] = $v_data['size'];
  5116     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size of Central Dir : \''.$p_central_dir['size'].'\'');
       
  5117     $p_central_dir['disk'] = $v_data['disk'];
  4657     $p_central_dir['disk'] = $v_data['disk'];
  5118     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Disk number : \''.$p_central_dir['disk'].'\'');
       
  5119     $p_central_dir['disk_start'] = $v_data['disk_start'];
  4658     $p_central_dir['disk_start'] = $v_data['disk_start'];
  5120     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Start disk number : \''.$p_central_dir['disk_start'].'\'');
       
  5121 
  4659 
  5122     // TBC
  4660     // TBC
  5123     //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
  4661     //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
  5124     //  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "central_dir[$key] = ".$p_central_dir[$key]);
       
  5125     //}
  4662     //}
  5126 
  4663 
  5127     // ----- Return
  4664     // ----- Return
  5128     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5129     return $v_result;
  4665     return $v_result;
  5130   }
  4666   }
  5131   // --------------------------------------------------------------------------------
  4667   // --------------------------------------------------------------------------------
  5132 
  4668 
  5133   // --------------------------------------------------------------------------------
  4669   // --------------------------------------------------------------------------------
  5136   // Parameters :
  4672   // Parameters :
  5137   // Return Values :
  4673   // Return Values :
  5138   // --------------------------------------------------------------------------------
  4674   // --------------------------------------------------------------------------------
  5139   function privDeleteByRule(&$p_result_list, &$p_options)
  4675   function privDeleteByRule(&$p_result_list, &$p_options)
  5140   {
  4676   {
  5141     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDeleteByRule", "");
       
  5142     $v_result=1;
  4677     $v_result=1;
  5143     $v_list_detail = array();
  4678     $v_list_detail = array();
  5144 
  4679 
  5145     // ----- Open the zip file
  4680     // ----- Open the zip file
  5146     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  5147     if (($v_result=$this->privOpenFd('rb')) != 1)
  4681     if (($v_result=$this->privOpenFd('rb')) != 1)
  5148     {
  4682     {
  5149       // ----- Return
  4683       // ----- Return
  5150       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5151       return $v_result;
  4684       return $v_result;
  5152     }
  4685     }
  5153 
  4686 
  5154     // ----- Read the central directory informations
  4687     // ----- Read the central directory informations
  5155     $v_central_dir = array();
  4688     $v_central_dir = array();
  5156     if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  4689     if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  5157     {
  4690     {
  5158       $this->privCloseFd();
  4691       $this->privCloseFd();
  5159       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5160       return $v_result;
  4692       return $v_result;
  5161     }
  4693     }
  5162 
  4694 
  5163     // ----- Go to beginning of File
  4695     // ----- Go to beginning of File
  5164     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
       
  5165     @rewind($this->zip_fd);
  4696     @rewind($this->zip_fd);
  5166     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
       
  5167 
  4697 
  5168     // ----- Scan all the files
  4698     // ----- Scan all the files
  5169     // ----- Start at beginning of Central Dir
  4699     // ----- Start at beginning of Central Dir
  5170     $v_pos_entry = $v_central_dir['offset'];
  4700     $v_pos_entry = $v_central_dir['offset'];
  5171     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
       
  5172     @rewind($this->zip_fd);
  4701     @rewind($this->zip_fd);
  5173     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
       
  5174     if (@fseek($this->zip_fd, $v_pos_entry))
  4702     if (@fseek($this->zip_fd, $v_pos_entry))
  5175     {
  4703     {
  5176       // ----- Close the zip file
  4704       // ----- Close the zip file
  5177       $this->privCloseFd();
  4705       $this->privCloseFd();
  5178 
  4706 
  5179       // ----- Error log
  4707       // ----- Error log
  5180       PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  4708       PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  5181 
  4709 
  5182       // ----- Return
  4710       // ----- Return
  5183       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  5184       return PclZip::errorCode();
  4711       return PclZip::errorCode();
  5185     }
  4712     }
  5186     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
       
  5187 
  4713 
  5188     // ----- Read each entry
  4714     // ----- Read each entry
  5189     $v_header_list = array();
  4715     $v_header_list = array();
  5190     $j_start = 0;
  4716     $j_start = 0;
  5191     for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  4717     for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  5192     {
  4718     {
  5193       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry (index '$i')");
       
  5194 
  4719 
  5195       // ----- Read the file header
  4720       // ----- Read the file header
  5196       $v_header_list[$v_nb_extracted] = array();
  4721       $v_header_list[$v_nb_extracted] = array();
  5197       if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
  4722       if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
  5198       {
  4723       {
  5199         // ----- Close the zip file
  4724         // ----- Close the zip file
  5200         $this->privCloseFd();
  4725         $this->privCloseFd();
  5201 
  4726 
  5202         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5203         return $v_result;
  4727         return $v_result;
  5204       }
  4728       }
  5205 
  4729 
  5206       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename (index '$i') : '".$v_header_list[$v_nb_extracted]['stored_filename']."'");
       
  5207 
  4730 
  5208       // ----- Store the index
  4731       // ----- Store the index
  5209       $v_header_list[$v_nb_extracted]['index'] = $i;
  4732       $v_header_list[$v_nb_extracted]['index'] = $i;
  5210 
  4733 
  5211       // ----- Look for the specific extract rules
  4734       // ----- Look for the specific extract rules
  5212       $v_found = false;
  4735       $v_found = false;
  5213 
  4736 
  5214       // ----- Look for extract by name rule
  4737       // ----- Look for extract by name rule
  5215       if (   (isset($p_options[PCLZIP_OPT_BY_NAME]))
  4738       if (   (isset($p_options[PCLZIP_OPT_BY_NAME]))
  5216           && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  4739           && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  5217           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
       
  5218 
  4740 
  5219           // ----- Look if the filename is in the list
  4741           // ----- Look if the filename is in the list
  5220           for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
  4742           for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
  5221               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
       
  5222 
  4743 
  5223               // ----- Look for a directory
  4744               // ----- Look for a directory
  5224               if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  4745               if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  5225                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
       
  5226 
  4746 
  5227                   // ----- Look if the directory is in the filename path
  4747                   // ----- Look if the directory is in the filename path
  5228                   if (   (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  4748                   if (   (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  5229                       && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  4749                       && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  5230                       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
       
  5231                       $v_found = true;
  4750                       $v_found = true;
  5232                   }
  4751                   }
  5233                   elseif (   (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
  4752                   elseif (   (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
  5234                           && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  4753                           && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  5235                       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The entry is the searched directory");
       
  5236                       $v_found = true;
  4754                       $v_found = true;
  5237                   }
  4755                   }
  5238               }
  4756               }
  5239               // ----- Look for a filename
  4757               // ----- Look for a filename
  5240               elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  4758               elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  5241                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
       
  5242                   $v_found = true;
  4759                   $v_found = true;
  5243               }
  4760               }
  5244           }
  4761           }
  5245       }
  4762       }
  5246 
  4763 
  5247       // ----- Look for extract by ereg rule
  4764       // ----- Look for extract by ereg rule
       
  4765       // ereg() is deprecated with PHP 5.3
       
  4766       /*
  5248       else if (   (isset($p_options[PCLZIP_OPT_BY_EREG]))
  4767       else if (   (isset($p_options[PCLZIP_OPT_BY_EREG]))
  5249                && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  4768                && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  5250           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
       
  5251 
  4769 
  5252           if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  4770           if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  5253               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
       
  5254               $v_found = true;
  4771               $v_found = true;
  5255           }
  4772           }
  5256       }
  4773       }
       
  4774       */
  5257 
  4775 
  5258       // ----- Look for extract by preg rule
  4776       // ----- Look for extract by preg rule
  5259       else if (   (isset($p_options[PCLZIP_OPT_BY_PREG]))
  4777       else if (   (isset($p_options[PCLZIP_OPT_BY_PREG]))
  5260                && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  4778                && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  5261           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
       
  5262 
  4779 
  5263           if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  4780           if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  5264               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
       
  5265               $v_found = true;
  4781               $v_found = true;
  5266           }
  4782           }
  5267       }
  4783       }
  5268 
  4784 
  5269       // ----- Look for extract by index rule
  4785       // ----- Look for extract by index rule
  5270       else if (   (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  4786       else if (   (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  5271                && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  4787                && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  5272           //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
       
  5273 
  4788 
  5274           // ----- Look if the index is in the list
  4789           // ----- Look if the index is in the list
  5275           for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
  4790           for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
  5276               //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look if index '$i' is in [".$p_options[PCLZIP_OPT_BY_INDEX][$j]['start'].",".$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']."]");
       
  5277 
  4791 
  5278               if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  4792               if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  5279                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
       
  5280                   $v_found = true;
  4793                   $v_found = true;
  5281               }
  4794               }
  5282               if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  4795               if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  5283                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
       
  5284                   $j_start = $j+1;
  4796                   $j_start = $j+1;
  5285               }
  4797               }
  5286 
  4798 
  5287               if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  4799               if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  5288                   //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
       
  5289                   break;
  4800                   break;
  5290               }
  4801               }
  5291           }
  4802           }
  5292       }
  4803       }
  5293       else {
  4804       else {
  5294         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "No argument mean remove all file");
       
  5295       	$v_found = true;
  4805       	$v_found = true;
  5296       }
  4806       }
  5297 
  4807 
  5298       // ----- Look for deletion
  4808       // ----- Look for deletion
  5299       if ($v_found)
  4809       if ($v_found)
  5300       {
  4810       {
  5301         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' need to be deleted");
       
  5302         unset($v_header_list[$v_nb_extracted]);
  4811         unset($v_header_list[$v_nb_extracted]);
  5303       }
  4812       }
  5304       else
  4813       else
  5305       {
  4814       {
  5306         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' will not be deleted");
       
  5307         $v_nb_extracted++;
  4815         $v_nb_extracted++;
  5308       }
  4816       }
  5309     }
  4817     }
  5310 
  4818 
  5311     // ----- Look if something need to be deleted
  4819     // ----- Look if something need to be deleted
  5316 
  4824 
  5317         // ----- Creates a temporary zip archive
  4825         // ----- Creates a temporary zip archive
  5318         $v_temp_zip = new PclZip($v_zip_temp_name);
  4826         $v_temp_zip = new PclZip($v_zip_temp_name);
  5319 
  4827 
  5320         // ----- Open the temporary zip file in write mode
  4828         // ----- Open the temporary zip file in write mode
  5321         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary write mode");
       
  5322         if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
  4829         if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
  5323             $this->privCloseFd();
  4830             $this->privCloseFd();
  5324 
  4831 
  5325             // ----- Return
  4832             // ----- Return
  5326             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5327             return $v_result;
  4833             return $v_result;
  5328         }
  4834         }
  5329 
  4835 
  5330         // ----- Look which file need to be kept
  4836         // ----- Look which file need to be kept
  5331         for ($i=0; $i<sizeof($v_header_list); $i++) {
  4837         for ($i=0; $i<sizeof($v_header_list); $i++) {
  5332             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Keep entry index '$i' : '".$v_header_list[$i]['filename']."'");
       
  5333 
  4838 
  5334             // ----- Calculate the position of the header
  4839             // ----- Calculate the position of the header
  5335             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset='". $v_header_list[$i]['offset']."'");
       
  5336             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
       
  5337             @rewind($this->zip_fd);
  4840             @rewind($this->zip_fd);
  5338             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
       
  5339             if (@fseek($this->zip_fd,  $v_header_list[$i]['offset'])) {
  4841             if (@fseek($this->zip_fd,  $v_header_list[$i]['offset'])) {
  5340                 // ----- Close the zip file
  4842                 // ----- Close the zip file
  5341                 $this->privCloseFd();
  4843                 $this->privCloseFd();
  5342                 $v_temp_zip->privCloseFd();
  4844                 $v_temp_zip->privCloseFd();
  5343                 @unlink($v_zip_temp_name);
  4845                 @unlink($v_zip_temp_name);
  5344 
  4846 
  5345                 // ----- Error log
  4847                 // ----- Error log
  5346                 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  4848                 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  5347 
  4849 
  5348                 // ----- Return
  4850                 // ----- Return
  5349                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  5350                 return PclZip::errorCode();
  4851                 return PclZip::errorCode();
  5351             }
  4852             }
  5352             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
       
  5353 
  4853 
  5354             // ----- Read the file header
  4854             // ----- Read the file header
  5355             $v_local_header = array();
  4855             $v_local_header = array();
  5356             if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
  4856             if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
  5357                 // ----- Close the zip file
  4857                 // ----- Close the zip file
  5358                 $this->privCloseFd();
  4858                 $this->privCloseFd();
  5359                 $v_temp_zip->privCloseFd();
  4859                 $v_temp_zip->privCloseFd();
  5360                 @unlink($v_zip_temp_name);
  4860                 @unlink($v_zip_temp_name);
  5361 
  4861 
  5362                 // ----- Return
  4862                 // ----- Return
  5363                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5364                 return $v_result;
  4863                 return $v_result;
  5365             }
  4864             }
  5366             
  4865 
  5367             // ----- Check that local file header is same as central file header
  4866             // ----- Check that local file header is same as central file header
  5368             if ($this->privCheckFileHeaders($v_local_header,
  4867             if ($this->privCheckFileHeaders($v_local_header,
  5369 			                                $v_header_list[$i]) != 1) {
  4868 			                                $v_header_list[$i]) != 1) {
  5370                 // TBC
  4869                 // TBC
  5371             }
  4870             }
  5377                 $this->privCloseFd();
  4876                 $this->privCloseFd();
  5378                 $v_temp_zip->privCloseFd();
  4877                 $v_temp_zip->privCloseFd();
  5379                 @unlink($v_zip_temp_name);
  4878                 @unlink($v_zip_temp_name);
  5380 
  4879 
  5381                 // ----- Return
  4880                 // ----- Return
  5382                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5383                 return $v_result;
  4881                 return $v_result;
  5384             }
  4882             }
  5385             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset for this file is '".$v_header_list[$i]['offset']."'");
       
  5386 
  4883 
  5387             // ----- Read/write the data block
  4884             // ----- Read/write the data block
  5388             if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
  4885             if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
  5389                 // ----- Close the zip file
  4886                 // ----- Close the zip file
  5390                 $this->privCloseFd();
  4887                 $this->privCloseFd();
  5391                 $v_temp_zip->privCloseFd();
  4888                 $v_temp_zip->privCloseFd();
  5392                 @unlink($v_zip_temp_name);
  4889                 @unlink($v_zip_temp_name);
  5393 
  4890 
  5394                 // ----- Return
  4891                 // ----- Return
  5395                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5396                 return $v_result;
  4892                 return $v_result;
  5397             }
  4893             }
  5398         }
  4894         }
  5399 
  4895 
  5400         // ----- Store the offset of the central dir
  4896         // ----- Store the offset of the central dir
  5401         $v_offset = @ftell($v_temp_zip->zip_fd);
  4897         $v_offset = @ftell($v_temp_zip->zip_fd);
  5402         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "New offset of central dir : $v_offset");
       
  5403 
  4898 
  5404         // ----- Re-Create the Central Dir files header
  4899         // ----- Re-Create the Central Dir files header
  5405         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the new central directory");
       
  5406         for ($i=0; $i<sizeof($v_header_list); $i++) {
  4900         for ($i=0; $i<sizeof($v_header_list); $i++) {
  5407             // ----- Create the file header
  4901             // ----- Create the file header
  5408             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset of file : ".$v_header_list[$i]['offset']);
       
  5409             if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  4902             if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  5410                 $v_temp_zip->privCloseFd();
  4903                 $v_temp_zip->privCloseFd();
  5411                 $this->privCloseFd();
  4904                 $this->privCloseFd();
  5412                 @unlink($v_zip_temp_name);
  4905                 @unlink($v_zip_temp_name);
  5413 
  4906 
  5414                 // ----- Return
  4907                 // ----- Return
  5415                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5416                 return $v_result;
  4908                 return $v_result;
  5417             }
  4909             }
  5418 
  4910 
  5419             // ----- Transform the header to a 'usable' info
  4911             // ----- Transform the header to a 'usable' info
  5420             $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  4912             $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  5421         }
  4913         }
  5422 
  4914 
  5423         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the central directory footer");
       
  5424 
  4915 
  5425         // ----- Zip file comment
  4916         // ----- Zip file comment
  5426         $v_comment = '';
  4917         $v_comment = '';
  5427         if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  4918         if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  5428           $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  4919           $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  5438             $v_temp_zip->privCloseFd();
  4929             $v_temp_zip->privCloseFd();
  5439             $this->privCloseFd();
  4930             $this->privCloseFd();
  5440             @unlink($v_zip_temp_name);
  4931             @unlink($v_zip_temp_name);
  5441 
  4932 
  5442             // ----- Return
  4933             // ----- Return
  5443             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5444             return $v_result;
  4934             return $v_result;
  5445         }
  4935         }
  5446 
  4936 
  5447         // ----- Close
  4937         // ----- Close
  5448         $v_temp_zip->privCloseFd();
  4938         $v_temp_zip->privCloseFd();
  5454 
  4944 
  5455         // ----- Rename the temporary file
  4945         // ----- Rename the temporary file
  5456         // TBC : I should test the result ...
  4946         // TBC : I should test the result ...
  5457         //@rename($v_zip_temp_name, $this->zipname);
  4947         //@rename($v_zip_temp_name, $this->zipname);
  5458         PclZipUtilRename($v_zip_temp_name, $this->zipname);
  4948         PclZipUtilRename($v_zip_temp_name, $this->zipname);
  5459     
  4949 
  5460         // ----- Destroy the temporary archive
  4950         // ----- Destroy the temporary archive
  5461         unset($v_temp_zip);
  4951         unset($v_temp_zip);
  5462     }
  4952     }
  5463     
  4953 
  5464     // ----- Remove every files : reset the file
  4954     // ----- Remove every files : reset the file
  5465     else if ($v_central_dir['entries'] != 0) {
  4955     else if ($v_central_dir['entries'] != 0) {
  5466         $this->privCloseFd();
  4956         $this->privCloseFd();
  5467 
  4957 
  5468         if (($v_result = $this->privOpenFd('wb')) != 1) {
  4958         if (($v_result = $this->privOpenFd('wb')) != 1) {
  5469           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5470           return $v_result;
  4959           return $v_result;
  5471         }
  4960         }
  5472 
  4961 
  5473         if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
  4962         if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
  5474           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5475           return $v_result;
  4963           return $v_result;
  5476         }
  4964         }
  5477 
  4965 
  5478         $this->privCloseFd();
  4966         $this->privCloseFd();
  5479     }
  4967     }
  5480 
  4968 
  5481     // ----- Return
  4969     // ----- Return
  5482     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5483     return $v_result;
  4970     return $v_result;
  5484   }
  4971   }
  5485   // --------------------------------------------------------------------------------
  4972   // --------------------------------------------------------------------------------
  5486 
  4973 
  5487   // --------------------------------------------------------------------------------
  4974   // --------------------------------------------------------------------------------
  5497   // --------------------------------------------------------------------------------
  4984   // --------------------------------------------------------------------------------
  5498   function privDirCheck($p_dir, $p_is_dir=false)
  4985   function privDirCheck($p_dir, $p_is_dir=false)
  5499   {
  4986   {
  5500     $v_result = 1;
  4987     $v_result = 1;
  5501 
  4988 
  5502     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDirCheck", "entry='$p_dir', is_dir='".($p_is_dir?"true":"false")."'");
       
  5503 
  4989 
  5504     // ----- Remove the final '/'
  4990     // ----- Remove the final '/'
  5505     if (($p_is_dir) && (substr($p_dir, -1)=='/'))
  4991     if (($p_is_dir) && (substr($p_dir, -1)=='/'))
  5506     {
  4992     {
  5507       $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
  4993       $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
  5508     }
  4994     }
  5509     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Looking for entry '$p_dir'");
       
  5510 
  4995 
  5511     // ----- Check the directory availability
  4996     // ----- Check the directory availability
  5512     if ((is_dir($p_dir)) || ($p_dir == ""))
  4997     if ((is_dir($p_dir)) || ($p_dir == ""))
  5513     {
  4998     {
  5514       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, "'$p_dir' is a directory");
       
  5515       return 1;
  4999       return 1;
  5516     }
  5000     }
  5517 
  5001 
  5518     // ----- Extract parent directory
  5002     // ----- Extract parent directory
  5519     $p_parent_dir = dirname($p_dir);
  5003     $p_parent_dir = dirname($p_dir);
  5520     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Parent directory is '$p_parent_dir'");
       
  5521 
  5004 
  5522     // ----- Just a check
  5005     // ----- Just a check
  5523     if ($p_parent_dir != $p_dir)
  5006     if ($p_parent_dir != $p_dir)
  5524     {
  5007     {
  5525       // ----- Look for parent directory
  5008       // ----- Look for parent directory
  5526       if ($p_parent_dir != "")
  5009       if ($p_parent_dir != "")
  5527       {
  5010       {
  5528         if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
  5011         if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
  5529         {
  5012         {
  5530           //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5531           return $v_result;
  5013           return $v_result;
  5532         }
  5014         }
  5533       }
  5015       }
  5534     }
  5016     }
  5535 
  5017 
  5536     // ----- Create the directory
  5018     // ----- Create the directory
  5537     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Create directory '$p_dir'");
       
  5538     if (!@mkdir($p_dir, 0777))
  5019     if (!@mkdir($p_dir, 0777))
  5539     {
  5020     {
  5540       // ----- Error log
  5021       // ----- Error log
  5541       PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
  5022       PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
  5542 
  5023 
  5543       // ----- Return
  5024       // ----- Return
  5544       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  5545       return PclZip::errorCode();
  5025       return PclZip::errorCode();
  5546     }
  5026     }
  5547 
  5027 
  5548     // ----- Return
  5028     // ----- Return
  5549     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result, "Directory '$p_dir' created");
       
  5550     return $v_result;
  5029     return $v_result;
  5551   }
  5030   }
  5552   // --------------------------------------------------------------------------------
  5031   // --------------------------------------------------------------------------------
  5553 
  5032 
  5554   // --------------------------------------------------------------------------------
  5033   // --------------------------------------------------------------------------------
  5558   // Parameters :
  5037   // Parameters :
  5559   // Return Values :
  5038   // Return Values :
  5560   // --------------------------------------------------------------------------------
  5039   // --------------------------------------------------------------------------------
  5561   function privMerge(&$p_archive_to_add)
  5040   function privMerge(&$p_archive_to_add)
  5562   {
  5041   {
  5563     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privMerge", "archive='".$p_archive_to_add->zipname."'");
       
  5564     $v_result=1;
  5042     $v_result=1;
  5565 
  5043 
  5566     // ----- Look if the archive_to_add exists
  5044     // ----- Look if the archive_to_add exists
  5567     if (!is_file($p_archive_to_add->zipname))
  5045     if (!is_file($p_archive_to_add->zipname))
  5568     {
  5046     {
  5569       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to add does not exist. End of merge.");
       
  5570 
  5047 
  5571       // ----- Nothing to merge, so merge is a success
  5048       // ----- Nothing to merge, so merge is a success
  5572       $v_result = 1;
  5049       $v_result = 1;
  5573 
  5050 
  5574       // ----- Return
  5051       // ----- Return
  5575       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5576       return $v_result;
  5052       return $v_result;
  5577     }
  5053     }
  5578 
  5054 
  5579     // ----- Look if the archive exists
  5055     // ----- Look if the archive exists
  5580     if (!is_file($this->zipname))
  5056     if (!is_file($this->zipname))
  5581     {
  5057     {
  5582       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, duplicate the archive_to_add.");
       
  5583 
  5058 
  5584       // ----- Do a duplicate
  5059       // ----- Do a duplicate
  5585       $v_result = $this->privDuplicate($p_archive_to_add->zipname);
  5060       $v_result = $this->privDuplicate($p_archive_to_add->zipname);
  5586 
  5061 
  5587       // ----- Return
  5062       // ----- Return
  5588       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5589       return $v_result;
  5063       return $v_result;
  5590     }
  5064     }
  5591 
  5065 
  5592     // ----- Open the zip file
  5066     // ----- Open the zip file
  5593     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  5594     if (($v_result=$this->privOpenFd('rb')) != 1)
  5067     if (($v_result=$this->privOpenFd('rb')) != 1)
  5595     {
  5068     {
  5596       // ----- Return
  5069       // ----- Return
  5597       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5598       return $v_result;
  5070       return $v_result;
  5599     }
  5071     }
  5600 
  5072 
  5601     // ----- Read the central directory informations
  5073     // ----- Read the central directory informations
  5602     $v_central_dir = array();
  5074     $v_central_dir = array();
  5603     if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  5075     if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  5604     {
  5076     {
  5605       $this->privCloseFd();
  5077       $this->privCloseFd();
  5606       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5607       return $v_result;
  5078       return $v_result;
  5608     }
  5079     }
  5609 
  5080 
  5610     // ----- Go to beginning of File
  5081     // ----- Go to beginning of File
  5611     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
       
  5612     @rewind($this->zip_fd);
  5082     @rewind($this->zip_fd);
  5613     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in zip : ".ftell($this->zip_fd)."'");
       
  5614 
  5083 
  5615     // ----- Open the archive_to_add file
  5084     // ----- Open the archive_to_add file
  5616     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open archive_to_add in binary read mode");
       
  5617     if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
  5085     if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
  5618     {
  5086     {
  5619       $this->privCloseFd();
  5087       $this->privCloseFd();
  5620 
  5088 
  5621       // ----- Return
  5089       // ----- Return
  5622       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5623       return $v_result;
  5090       return $v_result;
  5624     }
  5091     }
  5625 
  5092 
  5626     // ----- Read the central directory informations
  5093     // ----- Read the central directory informations
  5627     $v_central_dir_to_add = array();
  5094     $v_central_dir_to_add = array();
  5628     if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
  5095     if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
  5629     {
  5096     {
  5630       $this->privCloseFd();
  5097       $this->privCloseFd();
  5631       $p_archive_to_add->privCloseFd();
  5098       $p_archive_to_add->privCloseFd();
  5632 
  5099 
  5633       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5634       return $v_result;
  5100       return $v_result;
  5635     }
  5101     }
  5636 
  5102 
  5637     // ----- Go to beginning of File
  5103     // ----- Go to beginning of File
  5638     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
       
  5639     @rewind($p_archive_to_add->zip_fd);
  5104     @rewind($p_archive_to_add->zip_fd);
  5640     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in archive_to_add : ".ftell($p_archive_to_add->zip_fd)."'");
       
  5641 
  5105 
  5642     // ----- Creates a temporay file
  5106     // ----- Creates a temporay file
  5643     $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  5107     $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  5644 
  5108 
  5645     // ----- Open the temporary file in write mode
  5109     // ----- Open the temporary file in write mode
  5646     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  5647     if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  5110     if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  5648     {
  5111     {
  5649       $this->privCloseFd();
  5112       $this->privCloseFd();
  5650       $p_archive_to_add->privCloseFd();
  5113       $p_archive_to_add->privCloseFd();
  5651 
  5114 
  5652       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  5115       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  5653 
  5116 
  5654       // ----- Return
  5117       // ----- Return
  5655       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  5656       return PclZip::errorCode();
  5118       return PclZip::errorCode();
  5657     }
  5119     }
  5658 
  5120 
  5659     // ----- Copy the files from the archive to the temporary file
  5121     // ----- Copy the files from the archive to the temporary file
  5660     // TBC : Here I should better append the file and go back to erase the central dir
  5122     // TBC : Here I should better append the file and go back to erase the central dir
  5661     $v_size = $v_central_dir['offset'];
  5123     $v_size = $v_central_dir['offset'];
  5662     while ($v_size != 0)
  5124     while ($v_size != 0)
  5663     {
  5125     {
  5664       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5126       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5665       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  5666       $v_buffer = fread($this->zip_fd, $v_read_size);
  5127       $v_buffer = fread($this->zip_fd, $v_read_size);
  5667       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  5128       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  5668       $v_size -= $v_read_size;
  5129       $v_size -= $v_read_size;
  5669     }
  5130     }
  5670 
  5131 
  5671     // ----- Copy the files from the archive_to_add into the temporary file
  5132     // ----- Copy the files from the archive_to_add into the temporary file
  5672     $v_size = $v_central_dir_to_add['offset'];
  5133     $v_size = $v_central_dir_to_add['offset'];
  5673     while ($v_size != 0)
  5134     while ($v_size != 0)
  5674     {
  5135     {
  5675       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5136       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5676       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  5677       $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
  5137       $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
  5678       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  5138       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  5679       $v_size -= $v_read_size;
  5139       $v_size -= $v_read_size;
  5680     }
  5140     }
  5681 
  5141 
  5682     // ----- Store the offset of the central dir
  5142     // ----- Store the offset of the central dir
  5683     $v_offset = @ftell($v_zip_temp_fd);
  5143     $v_offset = @ftell($v_zip_temp_fd);
  5684     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
       
  5685 
  5144 
  5686     // ----- Copy the block of file headers from the old archive
  5145     // ----- Copy the block of file headers from the old archive
  5687     $v_size = $v_central_dir['size'];
  5146     $v_size = $v_central_dir['size'];
  5688     while ($v_size != 0)
  5147     while ($v_size != 0)
  5689     {
  5148     {
  5690       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5149       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5691       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  5692       $v_buffer = @fread($this->zip_fd, $v_read_size);
  5150       $v_buffer = @fread($this->zip_fd, $v_read_size);
  5693       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  5151       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  5694       $v_size -= $v_read_size;
  5152       $v_size -= $v_read_size;
  5695     }
  5153     }
  5696 
  5154 
  5697     // ----- Copy the block of file headers from the archive_to_add
  5155     // ----- Copy the block of file headers from the archive_to_add
  5698     $v_size = $v_central_dir_to_add['size'];
  5156     $v_size = $v_central_dir_to_add['size'];
  5699     while ($v_size != 0)
  5157     while ($v_size != 0)
  5700     {
  5158     {
  5701       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5159       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5702       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  5703       $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
  5160       $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
  5704       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  5161       @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  5705       $v_size -= $v_read_size;
  5162       $v_size -= $v_read_size;
  5706     }
  5163     }
  5707 
  5164 
  5728 
  5185 
  5729       // ----- Reset the file list
  5186       // ----- Reset the file list
  5730       unset($v_header_list);
  5187       unset($v_header_list);
  5731 
  5188 
  5732       // ----- Return
  5189       // ----- Return
  5733       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5734       return $v_result;
  5190       return $v_result;
  5735     }
  5191     }
  5736 
  5192 
  5737     // ----- Swap back the file descriptor
  5193     // ----- Swap back the file descriptor
  5738     $v_swap = $this->zip_fd;
  5194     $v_swap = $this->zip_fd;
  5754     // TBC : I should test the result ...
  5210     // TBC : I should test the result ...
  5755     //@rename($v_zip_temp_name, $this->zipname);
  5211     //@rename($v_zip_temp_name, $this->zipname);
  5756     PclZipUtilRename($v_zip_temp_name, $this->zipname);
  5212     PclZipUtilRename($v_zip_temp_name, $this->zipname);
  5757 
  5213 
  5758     // ----- Return
  5214     // ----- Return
  5759     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5760     return $v_result;
  5215     return $v_result;
  5761   }
  5216   }
  5762   // --------------------------------------------------------------------------------
  5217   // --------------------------------------------------------------------------------
  5763 
  5218 
  5764   // --------------------------------------------------------------------------------
  5219   // --------------------------------------------------------------------------------
  5767   // Parameters :
  5222   // Parameters :
  5768   // Return Values :
  5223   // Return Values :
  5769   // --------------------------------------------------------------------------------
  5224   // --------------------------------------------------------------------------------
  5770   function privDuplicate($p_archive_filename)
  5225   function privDuplicate($p_archive_filename)
  5771   {
  5226   {
  5772     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDuplicate", "archive_filename='$p_archive_filename'");
       
  5773     $v_result=1;
  5227     $v_result=1;
  5774 
  5228 
  5775     // ----- Look if the $p_archive_filename exists
  5229     // ----- Look if the $p_archive_filename exists
  5776     if (!is_file($p_archive_filename))
  5230     if (!is_file($p_archive_filename))
  5777     {
  5231     {
  5778       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive to duplicate does not exist. End of duplicate.");
       
  5779 
  5232 
  5780       // ----- Nothing to duplicate, so duplicate is a success.
  5233       // ----- Nothing to duplicate, so duplicate is a success.
  5781       $v_result = 1;
  5234       $v_result = 1;
  5782 
  5235 
  5783       // ----- Return
  5236       // ----- Return
  5784       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5785       return $v_result;
  5237       return $v_result;
  5786     }
  5238     }
  5787 
  5239 
  5788     // ----- Open the zip file
  5240     // ----- Open the zip file
  5789     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  5790     if (($v_result=$this->privOpenFd('wb')) != 1)
  5241     if (($v_result=$this->privOpenFd('wb')) != 1)
  5791     {
  5242     {
  5792       // ----- Return
  5243       // ----- Return
  5793       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5794       return $v_result;
  5244       return $v_result;
  5795     }
  5245     }
  5796 
  5246 
  5797     // ----- Open the temporary file in write mode
  5247     // ----- Open the temporary file in write mode
  5798     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
       
  5799     if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
  5248     if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
  5800     {
  5249     {
  5801       $this->privCloseFd();
  5250       $this->privCloseFd();
  5802 
  5251 
  5803       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
  5252       PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
  5804 
  5253 
  5805       // ----- Return
  5254       // ----- Return
  5806       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
       
  5807       return PclZip::errorCode();
  5255       return PclZip::errorCode();
  5808     }
  5256     }
  5809 
  5257 
  5810     // ----- Copy the files from the archive to the temporary file
  5258     // ----- Copy the files from the archive to the temporary file
  5811     // TBC : Here I should better append the file and go back to erase the central dir
  5259     // TBC : Here I should better append the file and go back to erase the central dir
  5812     $v_size = filesize($p_archive_filename);
  5260     $v_size = filesize($p_archive_filename);
  5813     while ($v_size != 0)
  5261     while ($v_size != 0)
  5814     {
  5262     {
  5815       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5263       $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  5816       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Read $v_read_size bytes");
       
  5817       $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  5264       $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  5818       @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  5265       @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  5819       $v_size -= $v_read_size;
  5266       $v_size -= $v_read_size;
  5820     }
  5267     }
  5821 
  5268 
  5824 
  5271 
  5825     // ----- Close the temporary file
  5272     // ----- Close the temporary file
  5826     @fclose($v_zip_temp_fd);
  5273     @fclose($v_zip_temp_fd);
  5827 
  5274 
  5828     // ----- Return
  5275     // ----- Return
  5829     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5830     return $v_result;
  5276     return $v_result;
  5831   }
  5277   }
  5832   // --------------------------------------------------------------------------------
  5278   // --------------------------------------------------------------------------------
  5833 
  5279 
  5834   // --------------------------------------------------------------------------------
  5280   // --------------------------------------------------------------------------------
  5871   // Parameters :
  5317   // Parameters :
  5872   // Return Values :
  5318   // Return Values :
  5873   // --------------------------------------------------------------------------------
  5319   // --------------------------------------------------------------------------------
  5874   function privDisableMagicQuotes()
  5320   function privDisableMagicQuotes()
  5875   {
  5321   {
  5876     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privDisableMagicQuotes', "");
       
  5877     $v_result=1;
  5322     $v_result=1;
  5878 
  5323 
  5879     // ----- Look if function exists
  5324     // ----- Look if function exists
  5880     if (   (!function_exists("get_magic_quotes_runtime"))
  5325     if (   (!function_exists("get_magic_quotes_runtime"))
  5881 	    || (!function_exists("set_magic_quotes_runtime"))) {
  5326 	    || (!function_exists("set_magic_quotes_runtime"))) {
  5882       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Functions *et_magic_quotes_runtime are not supported");
       
  5883       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5884       return $v_result;
  5327       return $v_result;
  5885 	}
  5328 	}
  5886 
  5329 
  5887     // ----- Look if already done
  5330     // ----- Look if already done
  5888     if ($this->magic_quotes_status != -1) {
  5331     if ($this->magic_quotes_status != -1) {
  5889       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "magic_quote already disabled");
       
  5890       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5891       return $v_result;
  5332       return $v_result;
  5892 	}
  5333 	}
  5893 
  5334 
  5894 	// ----- Get and memorize the magic_quote value
  5335 	// ----- Get and memorize the magic_quote value
  5895 	$this->magic_quotes_status = @get_magic_quotes_runtime();
  5336 	$this->magic_quotes_status = @get_magic_quotes_runtime();
  5896     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Current magic_quotes_runtime status is '".($this->magic_quotes_status==0?'disable':'enable')."'");
       
  5897 
  5337 
  5898 	// ----- Disable magic_quotes
  5338 	// ----- Disable magic_quotes
  5899 	if ($this->magic_quotes_status == 1) {
  5339 	if ($this->magic_quotes_status == 1) {
  5900       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Disable magic_quotes");
       
  5901 	  @set_magic_quotes_runtime(0);
  5340 	  @set_magic_quotes_runtime(0);
  5902 	}
  5341 	}
  5903 
  5342 
  5904     // ----- Return
  5343     // ----- Return
  5905     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5906     return $v_result;
  5344     return $v_result;
  5907   }
  5345   }
  5908   // --------------------------------------------------------------------------------
  5346   // --------------------------------------------------------------------------------
  5909 
  5347 
  5910   // --------------------------------------------------------------------------------
  5348   // --------------------------------------------------------------------------------
  5913   // Parameters :
  5351   // Parameters :
  5914   // Return Values :
  5352   // Return Values :
  5915   // --------------------------------------------------------------------------------
  5353   // --------------------------------------------------------------------------------
  5916   function privSwapBackMagicQuotes()
  5354   function privSwapBackMagicQuotes()
  5917   {
  5355   {
  5918     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::privSwapBackMagicQuotes', "");
       
  5919     $v_result=1;
  5356     $v_result=1;
  5920 
  5357 
  5921     // ----- Look if function exists
  5358     // ----- Look if function exists
  5922     if (   (!function_exists("get_magic_quotes_runtime"))
  5359     if (   (!function_exists("get_magic_quotes_runtime"))
  5923 	    || (!function_exists("set_magic_quotes_runtime"))) {
  5360 	    || (!function_exists("set_magic_quotes_runtime"))) {
  5924       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Functions *et_magic_quotes_runtime are not supported");
       
  5925       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5926       return $v_result;
  5361       return $v_result;
  5927 	}
  5362 	}
  5928 
  5363 
  5929     // ----- Look if something to do
  5364     // ----- Look if something to do
  5930     if ($this->magic_quotes_status != -1) {
  5365     if ($this->magic_quotes_status != -1) {
  5931       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "magic_quote not modified");
       
  5932       //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5933       return $v_result;
  5366       return $v_result;
  5934 	}
  5367 	}
  5935 
  5368 
  5936 	// ----- Swap back magic_quotes
  5369 	// ----- Swap back magic_quotes
  5937 	if ($this->magic_quotes_status == 1) {
  5370 	if ($this->magic_quotes_status == 1) {
  5938       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Enable back magic_quotes");
       
  5939   	  @set_magic_quotes_runtime($this->magic_quotes_status);
  5371   	  @set_magic_quotes_runtime($this->magic_quotes_status);
  5940 	}
  5372 	}
  5941 
  5373 
  5942     // ----- Return
  5374     // ----- Return
  5943     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  5944     return $v_result;
  5375     return $v_result;
  5945   }
  5376   }
  5946   // --------------------------------------------------------------------------------
  5377   // --------------------------------------------------------------------------------
  5947 
  5378 
  5948   }
  5379   }
  5955   // Parameters :
  5386   // Parameters :
  5956   // Return Values :
  5387   // Return Values :
  5957   // --------------------------------------------------------------------------------
  5388   // --------------------------------------------------------------------------------
  5958   function PclZipUtilPathReduction($p_dir)
  5389   function PclZipUtilPathReduction($p_dir)
  5959   {
  5390   {
  5960     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathReduction", "dir='$p_dir'");
       
  5961     $v_result = "";
  5391     $v_result = "";
  5962 
  5392 
  5963     // ----- Look for not empty path
  5393     // ----- Look for not empty path
  5964     if ($p_dir != "") {
  5394     if ($p_dir != "") {
  5965       // ----- Explode path by directory names
  5395       // ----- Explode path by directory names
  5982             $v_result = "/".$v_result;
  5412             $v_result = "/".$v_result;
  5983 		    if ($v_skip > 0) {
  5413 		    if ($v_skip > 0) {
  5984 		        // ----- It is an invalid path, so the path is not modified
  5414 		        // ----- It is an invalid path, so the path is not modified
  5985 		        // TBC
  5415 		        // TBC
  5986 		        $v_result = $p_dir;
  5416 		        $v_result = $p_dir;
  5987                 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid path is unchanged");
       
  5988                 $v_skip = 0;
  5417                 $v_skip = 0;
  5989 		    }
  5418 		    }
  5990 		  }
  5419 		  }
  5991 		  // ----- Last '/' i.e. indicates a directory
  5420 		  // ----- Last '/' i.e. indicates a directory
  5992 		  else if ($i == (sizeof($v_list)-1)) {
  5421 		  else if ($i == (sizeof($v_list)-1)) {
  6006 		  else {
  5435 		  else {
  6007             $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  5436             $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  6008 		  }
  5437 		  }
  6009         }
  5438         }
  6010       }
  5439       }
  6011       
  5440 
  6012       // ----- Look for skip
  5441       // ----- Look for skip
  6013       if ($v_skip > 0) {
  5442       if ($v_skip > 0) {
  6014         while ($v_skip > 0) {
  5443         while ($v_skip > 0) {
  6015             $v_result = '../'.$v_result;
  5444             $v_result = '../'.$v_result;
  6016             $v_skip--;
  5445             $v_skip--;
  6017         }
  5446         }
  6018       }
  5447       }
  6019     }
  5448     }
  6020 
  5449 
  6021     // ----- Return
  5450     // ----- Return
  6022     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  6023     return $v_result;
  5451     return $v_result;
  6024   }
  5452   }
  6025   // --------------------------------------------------------------------------------
  5453   // --------------------------------------------------------------------------------
  6026 
  5454 
  6027   // --------------------------------------------------------------------------------
  5455   // --------------------------------------------------------------------------------
  6039   //   1 if $p_path is inside directory $p_dir
  5467   //   1 if $p_path is inside directory $p_dir
  6040   //   2 if $p_path is exactly the same as $p_dir
  5468   //   2 if $p_path is exactly the same as $p_dir
  6041   // --------------------------------------------------------------------------------
  5469   // --------------------------------------------------------------------------------
  6042   function PclZipUtilPathInclusion($p_dir, $p_path)
  5470   function PclZipUtilPathInclusion($p_dir, $p_path)
  6043   {
  5471   {
  6044     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilPathInclusion", "dir='$p_dir', path='$p_path'");
       
  6045     $v_result = 1;
  5472     $v_result = 1;
  6046     
  5473 
  6047     // ----- Look for path beginning by ./
  5474     // ----- Look for path beginning by ./
  6048     if (   ($p_dir == '.')
  5475     if (   ($p_dir == '.')
  6049         || ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
  5476         || ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
  6050       $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
  5477       $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
  6051       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Replacing ./ by full path in p_dir '".$p_dir."'");
       
  6052     }
  5478     }
  6053     if (   ($p_path == '.')
  5479     if (   ($p_path == '.')
  6054         || ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
  5480         || ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
  6055       $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
  5481       $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
  6056       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Replacing ./ by full path in p_path '".$p_path."'");
       
  6057     }
  5482     }
  6058 
  5483 
  6059     // ----- Explode dir and path by directory separator
  5484     // ----- Explode dir and path by directory separator
  6060     $v_list_dir = explode("/", $p_dir);
  5485     $v_list_dir = explode("/", $p_dir);
  6061     $v_list_dir_size = sizeof($v_list_dir);
  5486     $v_list_dir_size = sizeof($v_list_dir);
  6064 
  5489 
  6065     // ----- Study directories paths
  5490     // ----- Study directories paths
  6066     $i = 0;
  5491     $i = 0;
  6067     $j = 0;
  5492     $j = 0;
  6068     while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  5493     while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  6069       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Working on dir($i)='".$v_list_dir[$i]."' and path($j)='".$v_list_path[$j]."'");
       
  6070 
  5494 
  6071       // ----- Look for empty dir (path reduction)
  5495       // ----- Look for empty dir (path reduction)
  6072       if ($v_list_dir[$i] == '') {
  5496       if ($v_list_dir[$i] == '') {
  6073         $i++;
  5497         $i++;
  6074         continue;
  5498         continue;
  6078         continue;
  5502         continue;
  6079       }
  5503       }
  6080 
  5504 
  6081       // ----- Compare the items
  5505       // ----- Compare the items
  6082       if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != ''))  {
  5506       if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != ''))  {
  6083         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Items ($i,$j) are different");
       
  6084         $v_result = 0;
  5507         $v_result = 0;
  6085       }
  5508       }
  6086 
  5509 
  6087       // ----- Next items
  5510       // ----- Next items
  6088       $i++;
  5511       $i++;
  6089       $j++;
  5512       $j++;
  6090     }
  5513     }
  6091 
  5514 
  6092     // ----- Look if everything seems to be the same
  5515     // ----- Look if everything seems to be the same
  6093     if ($v_result) {
  5516     if ($v_result) {
  6094       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Look for tie break");
       
  6095       // ----- Skip all the empty items
  5517       // ----- Skip all the empty items
  6096       while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
  5518       while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
  6097       while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
  5519       while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
  6098       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Looking on dir($i)='".($i < $v_list_dir_size?$v_list_dir[$i]:'')."' and path($j)='".($j < $v_list_path_size?$v_list_path[$j]:'')."'");
       
  6099 
  5520 
  6100       if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  5521       if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  6101         // ----- There are exactly the same
  5522         // ----- There are exactly the same
  6102         $v_result = 2;
  5523         $v_result = 2;
  6103       }
  5524       }
  6106         $v_result = 0;
  5527         $v_result = 0;
  6107       }
  5528       }
  6108     }
  5529     }
  6109 
  5530 
  6110     // ----- Return
  5531     // ----- Return
  6111     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  6112     return $v_result;
  5532     return $v_result;
  6113   }
  5533   }
  6114   // --------------------------------------------------------------------------------
  5534   // --------------------------------------------------------------------------------
  6115 
  5535 
  6116   // --------------------------------------------------------------------------------
  5536   // --------------------------------------------------------------------------------
  6124   //             3 : src & dest gzip
  5544   //             3 : src & dest gzip
  6125   // Return Values :
  5545   // Return Values :
  6126   // --------------------------------------------------------------------------------
  5546   // --------------------------------------------------------------------------------
  6127   function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
  5547   function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
  6128   {
  5548   {
  6129     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilCopyBlock", "size=$p_size, mode=$p_mode");
       
  6130     $v_result = 1;
  5549     $v_result = 1;
  6131 
  5550 
  6132     if ($p_mode==0)
  5551     if ($p_mode==0)
  6133     {
  5552     {
  6134       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset before read :".(@ftell($p_src)));
       
  6135       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset before write :".(@ftell($p_dest)));
       
  6136       while ($p_size != 0)
  5553       while ($p_size != 0)
  6137       {
  5554       {
  6138         $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  5555         $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  6139         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  6140         $v_buffer = @fread($p_src, $v_read_size);
  5556         $v_buffer = @fread($p_src, $v_read_size);
  6141         @fwrite($p_dest, $v_buffer, $v_read_size);
  5557         @fwrite($p_dest, $v_buffer, $v_read_size);
  6142         $p_size -= $v_read_size;
  5558         $p_size -= $v_read_size;
  6143       }
  5559       }
  6144       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Src offset after read :".(@ftell($p_src)));
       
  6145       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Dest offset after write :".(@ftell($p_dest)));
       
  6146     }
  5560     }
  6147     else if ($p_mode==1)
  5561     else if ($p_mode==1)
  6148     {
  5562     {
  6149       while ($p_size != 0)
  5563       while ($p_size != 0)
  6150       {
  5564       {
  6151         $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  5565         $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  6152         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  6153         $v_buffer = @gzread($p_src, $v_read_size);
  5566         $v_buffer = @gzread($p_src, $v_read_size);
  6154         @fwrite($p_dest, $v_buffer, $v_read_size);
  5567         @fwrite($p_dest, $v_buffer, $v_read_size);
  6155         $p_size -= $v_read_size;
  5568         $p_size -= $v_read_size;
  6156       }
  5569       }
  6157     }
  5570     }
  6158     else if ($p_mode==2)
  5571     else if ($p_mode==2)
  6159     {
  5572     {
  6160       while ($p_size != 0)
  5573       while ($p_size != 0)
  6161       {
  5574       {
  6162         $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  5575         $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  6163         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  6164         $v_buffer = @fread($p_src, $v_read_size);
  5576         $v_buffer = @fread($p_src, $v_read_size);
  6165         @gzwrite($p_dest, $v_buffer, $v_read_size);
  5577         @gzwrite($p_dest, $v_buffer, $v_read_size);
  6166         $p_size -= $v_read_size;
  5578         $p_size -= $v_read_size;
  6167       }
  5579       }
  6168     }
  5580     }
  6169     else if ($p_mode==3)
  5581     else if ($p_mode==3)
  6170     {
  5582     {
  6171       while ($p_size != 0)
  5583       while ($p_size != 0)
  6172       {
  5584       {
  6173         $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  5585         $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  6174         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
       
  6175         $v_buffer = @gzread($p_src, $v_read_size);
  5586         $v_buffer = @gzread($p_src, $v_read_size);
  6176         @gzwrite($p_dest, $v_buffer, $v_read_size);
  5587         @gzwrite($p_dest, $v_buffer, $v_read_size);
  6177         $p_size -= $v_read_size;
  5588         $p_size -= $v_read_size;
  6178       }
  5589       }
  6179     }
  5590     }
  6180 
  5591 
  6181     // ----- Return
  5592     // ----- Return
  6182     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  6183     return $v_result;
  5593     return $v_result;
  6184   }
  5594   }
  6185   // --------------------------------------------------------------------------------
  5595   // --------------------------------------------------------------------------------
  6186 
  5596 
  6187   // --------------------------------------------------------------------------------
  5597   // --------------------------------------------------------------------------------
  6196   // Return Values :
  5606   // Return Values :
  6197   //   1 on success, 0 on failure.
  5607   //   1 on success, 0 on failure.
  6198   // --------------------------------------------------------------------------------
  5608   // --------------------------------------------------------------------------------
  6199   function PclZipUtilRename($p_src, $p_dest)
  5609   function PclZipUtilRename($p_src, $p_dest)
  6200   {
  5610   {
  6201     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilRename", "source=$p_src, destination=$p_dest");
       
  6202     $v_result = 1;
  5611     $v_result = 1;
  6203 
  5612 
  6204     // ----- Try to rename the files
  5613     // ----- Try to rename the files
  6205     if (!@rename($p_src, $p_dest)) {
  5614     if (!@rename($p_src, $p_dest)) {
  6206       //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to rename file, try copy+unlink");
       
  6207 
  5615 
  6208       // ----- Try to copy & unlink the src
  5616       // ----- Try to copy & unlink the src
  6209       if (!@copy($p_src, $p_dest)) {
  5617       if (!@copy($p_src, $p_dest)) {
  6210         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to copy file");
       
  6211         $v_result = 0;
  5618         $v_result = 0;
  6212       }
  5619       }
  6213       else if (!@unlink($p_src)) {
  5620       else if (!@unlink($p_src)) {
  6214         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Fail to unlink old filename");
       
  6215         $v_result = 0;
  5621         $v_result = 0;
  6216       }
  5622       }
  6217     }
  5623     }
  6218 
  5624 
  6219     // ----- Return
  5625     // ----- Return
  6220     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  6221     return $v_result;
  5626     return $v_result;
  6222   }
  5627   }
  6223   // --------------------------------------------------------------------------------
  5628   // --------------------------------------------------------------------------------
  6224 
  5629 
  6225   // --------------------------------------------------------------------------------
  5630   // --------------------------------------------------------------------------------
  6231   // Return Values :
  5636   // Return Values :
  6232   //   The option text value.
  5637   //   The option text value.
  6233   // --------------------------------------------------------------------------------
  5638   // --------------------------------------------------------------------------------
  6234   function PclZipUtilOptionText($p_option)
  5639   function PclZipUtilOptionText($p_option)
  6235   {
  5640   {
  6236     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZipUtilOptionText", "option='".$p_option."'");
  5641 
  6237     
       
  6238     $v_list = get_defined_constants();
  5642     $v_list = get_defined_constants();
  6239     for (reset($v_list); $v_key = key($v_list); next($v_list)) {
  5643     for (reset($v_list); $v_key = key($v_list); next($v_list)) {
  6240 	    $v_prefix = substr($v_key, 0, 10);
  5644 	    $v_prefix = substr($v_key, 0, 10);
  6241 	    if ((   ($v_prefix == 'PCLZIP_OPT')
  5645 	    if ((   ($v_prefix == 'PCLZIP_OPT')
  6242            || ($v_prefix == 'PCLZIP_CB_')
  5646            || ($v_prefix == 'PCLZIP_CB_')
  6243            || ($v_prefix == 'PCLZIP_ATT'))
  5647            || ($v_prefix == 'PCLZIP_ATT'))
  6244 	        && ($v_list[$v_key] == $p_option)) {
  5648 	        && ($v_list[$v_key] == $p_option)) {
  6245         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_key);
       
  6246         return $v_key;
  5649         return $v_key;
  6247 	    }
  5650 	    }
  6248     }
  5651     }
  6249     
  5652 
  6250     $v_result = 'Unknown';
  5653     $v_result = 'Unknown';
  6251 
  5654 
  6252     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
       
  6253     return $v_result;
  5655     return $v_result;
  6254   }
  5656   }
  6255   // --------------------------------------------------------------------------------
  5657   // --------------------------------------------------------------------------------
  6256 
  5658 
  6257   // --------------------------------------------------------------------------------
  5659   // --------------------------------------------------------------------------------