Viewing file: pclzip.class.php (71.39 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php /** * @version $Id: pclzip.lib.php,v 1.2 2004/08/26 05:20:52 rcastley Exp $ * @package Mambo_4.5.1 */
// ------------------------------------ // PhpConcept Library - Zip Module 2.1 // ------------------------------------ // License GNU/LGPL - Vincent Blavet - December 2003 // http://www.phpconcept.net // ------------------------------------ // // Presentation : // PclZip is a PHP library that manage ZIP archives. // So far tests show that archives generated by PclZip are readable by // WinZip application and other tools. // // Description : // See readme.txt and http://www.phpconcept.net // // Warning : // This library and the associated files are non commercial, non professional // work. // It should not have unexpected results. However if any damage is caused by // this software the author can not be responsible. // The use of this software is at the risk of the user. // // Note: // I have removed some functions from this script which are not needed by // PHP Navigator. So if you want the full library, download it from the // above site - Cyril Sebastian // ------------------------------------ // $Id: pclzip.lib.php,v 1.2 2004/08/26 05:20:52 rcastley Exp $ // ------------------------------------
// ----- Constants define( 'PCLZIP_READ_BLOCK_SIZE', 2048 ); define( 'PCLZIP_SEPARATOR', ',' );
define( 'PCLZIP_ERROR_EXTERNAL', 0 ); define( 'PCLZIP_TEMPORARY_DIR', '' );
// ------------------------------------ // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED ***** // ------------------------------------
// ----- Global variables $g_pclzip_version = "2.1"; define( 'PCLZIP_ERR_USER_ABORTED', 2 ); define( 'PCLZIP_ERR_NO_ERROR', 0 ); define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 ); define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 ); define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 ); define( 'PCLZIP_ERR_MISSING_FILE', -4 ); define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 ); define( 'PCLZIP_ERR_INVALID_ZIP', -6 ); define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 ); define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 ); define( 'PCLZIP_ERR_BAD_EXTENSION', -9 ); define( 'PCLZIP_ERR_BAD_FORMAT', -10 ); define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 ); define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 ); define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 ); define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 ); define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 ); define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
// ----- Options values define( 'PCLZIP_OPT_PATH', 77001 ); define( 'PCLZIP_OPT_ADD_PATH', 77002 ); define( 'PCLZIP_OPT_REMOVE_PATH', 77003 ); define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 ); define( 'PCLZIP_OPT_SET_CHMOD', 77005 ); define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 ); define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 ); define( 'PCLZIP_OPT_BY_NAME', 77008 ); define( 'PCLZIP_OPT_BY_INDEX', 77009 ); define( 'PCLZIP_OPT_BY_EREG', 77010 ); define( 'PCLZIP_OPT_BY_PREG', 77011 ); define( 'PCLZIP_OPT_COMMENT', 77012 ); define( 'PCLZIP_OPT_ADD_COMMENT', 77013 ); define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 ); define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
// ----- Call backs values define( 'PCLZIP_CB_PRE_EXTRACT', 78001 ); define( 'PCLZIP_CB_POST_EXTRACT', 78002 ); define( 'PCLZIP_CB_PRE_ADD', 78003 ); define( 'PCLZIP_CB_POST_ADD', 78004 ); // ------------------------------------ // Class : PclZip class PclZip { // ----- Filename of the zip file var $zipname = '';
// ----- File descriptor of the zip file var $zip_fd = 0;
// ----- Internal error handling var $error_code = 1; var $error_string = '';
// ------------------------------------------- // Function : PclZip() function PclZip($p_zipname) { if (!function_exists('gzopen')) { die('Abort '.basename(__FILE__).' : Missing zlib extensions'); }
$this->zipname = $p_zipname; $this->zip_fd = 0;
return; }
// extract($p_path="./", $p_remove_path="") function extract(/* options */) { $v_result=1;
// ----- Reset the error handler $this->privErrorReset();
// ----- Check archive if (!$this->privCheckFormat()) { return(0); }
// ----- Set default values $v_options = array(); $v_path = "./"; $v_remove_path = ""; $v_remove_all_path = false;
$v_size = func_num_args();
$v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
if ($v_size > 0) { $v_arg_list = &func_get_args();
if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
$v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options, array (PCLZIP_OPT_PATH => 'optional', PCLZIP_OPT_REMOVE_PATH => 'optional', PCLZIP_OPT_REMOVE_ALL_PATH => 'optional', PCLZIP_OPT_ADD_PATH => 'optional', PCLZIP_CB_PRE_EXTRACT => 'optional', PCLZIP_CB_POST_EXTRACT => 'optional', PCLZIP_OPT_SET_CHMOD => 'optional', PCLZIP_OPT_BY_NAME => 'optional', PCLZIP_OPT_BY_EREG => 'optional', PCLZIP_OPT_BY_PREG => 'optional', PCLZIP_OPT_BY_INDEX => 'optional', PCLZIP_OPT_EXTRACT_AS_STRING => 'optional', PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional' )); if ($v_result != 1) { return 0; }
if (isset($v_options[PCLZIP_OPT_PATH])) { $v_path = $v_options[PCLZIP_OPT_PATH]; } if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) { $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH]; } if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) { $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH]; } if (isset($v_options[PCLZIP_OPT_ADD_PATH])) { if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) { $v_path .= '/'; } $v_path .= $v_options[PCLZIP_OPT_ADD_PATH]; } }
else {
$v_path = $v_arg_list[0]; if ($v_size == 2) { $v_remove_path = $v_arg_list[1]; } else if ($v_size > 2) { PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
return 0; } } }
$p_list = array(); $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options); if ($v_result < 1) { unset($p_list); return(0); }
return $p_list; }
// ------------------------------------ // Function : properties() function properties() {
$this->privErrorReset();
if (!$this->privCheckFormat()) { return(0); }
$v_prop = array(); $v_prop['comment'] = ''; $v_prop['nb'] = 0; $v_prop['status'] = 'not_exist';
if (@is_file($this->zipname)) { if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) { PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
return 0; }
$v_central_dir = array(); if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) { return 0; }
$this->privCloseFd();
$v_prop['comment'] = $v_central_dir['comment']; $v_prop['nb'] = $v_central_dir['entries']; $v_prop['status'] = 'ok'; } return $v_prop; }
// Function : listContent() // Description : function listContent() { $v_result=1; $this->privErrorReset();
if (!$this->privCheckFormat()) { return(0); }
$p_list = array(); if (($v_result = $this->privList($p_list)) != 1) { unset($p_list); return(0); }
return $p_list; }
// ------------------------------------ // Function : errorCode() // Description : // Parameters : // ------------------------------------ function errorCode() { if (PCLZIP_ERROR_EXTERNAL == 1) { return(PclErrorCode()); } else { return($this->error_code); } } // ------------------------------------
// ------------------------------------ // Function : errorName() // Description : // Parameters : // ------------------------------------ function errorName($p_with_code=false) { $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR', PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL', PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL', PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER', PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE', PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG', PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP', PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE', PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL', PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION', PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT', PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL', PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL', PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM', PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE', PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE' );
if (isset($v_name[$this->error_code])) { $v_value = $v_name[$this->error_code]; } else { $v_value = 'NoName'; }
if ($p_with_code) { return($v_value.' ('.$this->error_code.')'); } else { return($v_value); } } // ------------------------------------
// ------------------------------------ // Function : errorInfo() // Description : // Parameters : // ------------------------------------ function errorInfo($p_full=false) { if (PCLZIP_ERROR_EXTERNAL == 1) { return(PclErrorString()); } else { if ($p_full) { return($this->errorName(true)." : ".$this->error_string); } else { return($this->error_string." [code ".$this->error_code."]"); } } } // ------------------------------------
// ------------------------------------ // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS ***** // ***** ***** // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY ***** // ------------------------------------
// ------------------------------------ // Function : privCheckFormat() function privCheckFormat($p_level=0) { $v_result = true; clearstatcache();
$this->privErrorReset();
if (!is_file($this->zipname)) { PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'"); return(false); }
if (!is_readable($this->zipname)) { PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'"); return(false); }
return $v_result; }
// ------------------------------------ // Function : privParseOptions() function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false) { $v_result=1;
$i=0; while ($i<$p_size) {
if (!isset($v_requested_options[$p_options_list[$i]])) { PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
return PclZip::errorCode(); }
switch ($p_options_list[$i]) { case PCLZIP_OPT_PATH : case PCLZIP_OPT_REMOVE_PATH : case PCLZIP_OPT_ADD_PATH : if (($i+1) >= $p_size) { PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
return PclZip::errorCode(); }
$v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], false); $i++; break;
case PCLZIP_OPT_BY_NAME : if (($i+1) >= $p_size) { PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
return PclZip::errorCode(); }
if (is_string($p_options_list[$i+1])) { $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1]; } else if (is_array($p_options_list[$i+1])) { $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1]; } else { PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
return PclZip::errorCode(); } $i++; break;
case PCLZIP_OPT_BY_EREG : case PCLZIP_OPT_BY_PREG : if (($i+1) >= $p_size) { PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
return PclZip::errorCode(); }
if (is_string($p_options_list[$i+1])) { $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1]; } else { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
return PclZip::errorCode(); } $i++; break;
case PCLZIP_OPT_COMMENT : case PCLZIP_OPT_ADD_COMMENT : case PCLZIP_OPT_PREPEND_COMMENT : // ----- Check the number of parameters if (($i+1) >= $p_size) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" .PclZipUtilOptionText($p_options_list[$i]) ."'");
return PclZip::errorCode(); }
// ----- Get the value if (is_string($p_options_list[$i+1])) { $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1]; } else { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" .PclZipUtilOptionText($p_options_list[$i]) ."'");
return PclZip::errorCode(); } $i++; break;
case PCLZIP_OPT_BY_INDEX : // ----- Check the number of parameters if (($i+1) >= $p_size) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return return PclZip::errorCode(); }
// ----- Get the value $v_work_list = array(); if (is_string($p_options_list[$i+1])) {
// ----- Remove spaces $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
// ----- Parse items $v_work_list = explode(",", $p_options_list[$i+1]); } else if (is_integer($p_options_list[$i+1])) { $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1]; } else if (is_array($p_options_list[$i+1])) { $v_work_list = $p_options_list[$i+1]; } else { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return return PclZip::errorCode(); } $v_sort_flag=false; $v_sort_value=0; for ($j=0; $j<sizeof($v_work_list); $j++) { // ----- Explode the item $v_item_list = explode("-", $v_work_list[$j]); $v_size_item_list = sizeof($v_item_list); if ($v_size_item_list == 1) { // ----- Set the option value $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0]; $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0]; } elseif ($v_size_item_list == 2) { // ----- Set the option value $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0]; $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1]; } else { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return return PclZip::errorCode(); }
// ----- Look for list sort if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) { $v_sort_flag=true;
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return return PclZip::errorCode(); } $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start']; } // ----- Sort the items if ($v_sort_flag) { }
// ----- Next option $i++; break;
// ----- Look for options that request no value case PCLZIP_OPT_REMOVE_ALL_PATH : case PCLZIP_OPT_EXTRACT_AS_STRING : case PCLZIP_OPT_NO_COMPRESSION : case PCLZIP_OPT_EXTRACT_IN_OUTPUT : $v_result_list[$p_options_list[$i]] = true; break;
case PCLZIP_OPT_SET_CHMOD : // ----- Check the number of parameters if (($i+1) >= $p_size) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return return PclZip::errorCode(); }
// ----- Get the value $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1]; $i++; break;
// ----- Look for options that request a call-back case PCLZIP_CB_PRE_EXTRACT : case PCLZIP_CB_POST_EXTRACT : case PCLZIP_CB_PRE_ADD : case PCLZIP_CB_POST_ADD : // ----- Check the number of parameters if (($i+1) >= $p_size) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return return PclZip::errorCode(); }
// ----- Get the value $v_function_name = $p_options_list[$i+1];
// ----- Check that the value is a valid existing function if (!function_exists($v_function_name)) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
// ----- Return return PclZip::errorCode(); }
// ----- Set the attribute $v_result_list[$p_options_list[$i]] = $v_function_name; $i++; break;
default : // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Unknown parameter '" .$p_options_list[$i]."'");
return PclZip::errorCode(); }
// ----- Next options $i++; }
// ----- Look for mandatory options if ($v_requested_options !== false) { for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) { // ----- Look for mandatory option if ($v_requested_options[$key] == 'mandatory') { // ----- Look if present if (!isset($v_result_list[$key])) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
// ----- Return return PclZip::errorCode(); } } } }
// ----- Return return $v_result; } // ------------------------------------
// ------------------------------------ // Function : privOpenFd() // Description : // Parameters : // ------------------------------------ function privOpenFd($p_mode) { $v_result=1;
// ----- Look if already open if ($this->zip_fd != 0) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
// ----- Return return PclZip::errorCode(); }
// ----- Open the zip file if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
// ----- Return return PclZip::errorCode(); }
// ----- Return return $v_result; }
// ------------------------------------ // Function : privCloseFd() // Description : // Parameters : // ------------------------------------ function privCloseFd() { $v_result=1;
if ($this->zip_fd != 0) @fclose($this->zip_fd); $this->zip_fd = 0;
return $v_result; }
// ------------------------------------ // Function : privAddList() // Description : // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is // different from the real path of the file. This is usefull if you want to have PclTar // running in any directory, and memorize relative path from an other directory. // Parameters : // $p_list : An array containing the file or directory names to add in the tar // $p_result_list : list of added files with their properties (specially the status field) // $p_add_dir : Path to add in the filename path archived // $p_remove_dir : Path to remove in the filename path archived // Return Values : // ------------------------------------ function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options) { $v_result=1;
// ----- Add the files $v_header_list = array(); if (($v_result = $this->privAddFileList($p_list, $v_header_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1) { return $v_result; }
$v_offset = @ftell($this->zip_fd);
for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++) { if ($v_header_list[$i]['status'] == 'ok') { if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) { return $v_result; } $v_count++; }
$this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]); }
$v_comment = ''; if (isset($p_options[PCLZIP_OPT_COMMENT])) { $v_comment = $p_options[PCLZIP_OPT_COMMENT]; }
// ----- Calculate the size of the central header $v_size = @ftell($this->zip_fd)-$v_offset;
// ----- Create the central dir footer if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1) { // ----- Reset the file list unset($v_header_list);
// ----- Return return $v_result; }
// ----- Return return $v_result; }
// ------------------------------------ // Function : privAddFileList() // Description : // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is // different from the real path of the file. This is usefull if you want to // run the lib in any directory, and memorize relative path from an other directory. // Parameters : // $p_list : An array containing the file or directory names to add in the tar // $p_result_list : list of added files with their properties (specially the status field) // $p_add_dir : Path to add in the filename path archived // $p_remove_dir : Path to remove in the filename path archived // Return Values : // ------------------------------------ function privAddFileList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options) { $v_result=1; $v_header = array();
$v_nb = sizeof($p_result_list);
for ($j=0; ($j<count($p_list)) && ($v_result==1); $j++) { $p_filename = PclZipUtilTranslateWinPath($p_list[$j], false);
// ----- Skip empty file names if ($p_filename == "") { continue; }
// ----- Check the filename if (!file_exists($p_filename)) { PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '$p_filename' does not exists");
// ----- Return return PclZip::errorCode(); }
if (strlen($p_filename) > 0xFF) { // ----- Error log PclZip::privErrorLog(-5, "File name is too long (max. 255) : '$p_filename'");
// ----- Return return PclZip::errorCode(); }
if ((is_file($p_filename)) || ((is_dir($p_filename)) && !$p_remove_all_dir)) { // ----- Add the file if (($v_result = $this->privAddFile($p_filename, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1) { return $v_result; }
// ----- Store the file infos $p_result_list[$v_nb++] = $v_header; }
// ----- Look for directory if (is_dir($p_filename)) {
// ----- Look for path if ($p_filename != ".") $v_path = $p_filename."/"; else $v_path = "";
// ----- Read the directory for files and sub-directories $p_hdir = opendir($p_filename); $p_hitem = readdir($p_hdir); // '.' directory $p_hitem = readdir($p_hdir); // '..' directory while (($p_hitem = readdir($p_hdir)) !== false) {
// ----- Look for a file if (is_file($v_path.$p_hitem)) {
// ----- Add the file if (($v_result = $this->privAddFile($v_path.$p_hitem, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1) { // ----- Return status return $v_result; }
// ----- Store the file infos $p_result_list[$v_nb++] = $v_header; }
// ----- Recursive call to privAddFileList() else {
// ----- Need an array as parameter $p_temp_list[0] = $v_path.$p_hitem; $v_result = $this->privAddFileList($p_temp_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options);
// ----- Update the number of elements of the list $v_nb = sizeof($p_result_list); } }
// ----- Free memory for the recursive loop unset($p_temp_list); unset($p_hdir); unset($p_hitem); } }
// ----- Return return $v_result; }
// ------------------------------------ // Function : privAddFile() // Description : // Parameters : // Return Values : // ------------------------------------ function privAddFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options) { $v_result=1;
if ($p_filename == "") { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
// ----- Return return PclZip::errorCode(); }
// ----- Calculate the stored filename $v_stored_filename = $p_filename;
// ----- Look for all path to remove if ($p_remove_all_dir) { $v_stored_filename = basename($p_filename); } // ----- Look for partial path remove else if ($p_remove_dir != "") { if (substr($p_remove_dir, -1) != '/') $p_remove_dir .= "/";
if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./")) { if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./")) $p_remove_dir = "./".$p_remove_dir; if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./")) $p_remove_dir = substr($p_remove_dir, 2); }
$v_compare = PclZipUtilPathInclusion($p_remove_dir, $p_filename); if ($v_compare > 0) {
if ($v_compare == 2) { $v_stored_filename = ""; } else { $v_stored_filename = substr($p_filename, strlen($p_remove_dir)); } } } // ----- Look for path to add if ($p_add_dir != "") { if (substr($p_add_dir, -1) == "/") $v_stored_filename = $p_add_dir.$v_stored_filename; else $v_stored_filename = $p_add_dir."/".$v_stored_filename; }
// ----- Filename (reduce the path of stored name) $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
// ----- Set the file properties clearstatcache(); $p_header['version'] = 20; $p_header['version_extracted'] = 10; $p_header['flag'] = 0; $p_header['compression'] = 0; $p_header['mtime'] = filemtime($p_filename); $p_header['crc'] = 0; $p_header['compressed_size'] = 0; $p_header['size'] = filesize($p_filename); $p_header['filename_len'] = strlen($p_filename); $p_header['extra_len'] = 0; $p_header['comment_len'] = 0; $p_header['disk'] = 0; $p_header['internal'] = 0; $p_header['external'] = (is_file($p_filename)?0xFE49FFE0:0x41FF0010); $p_header['offset'] = 0; $p_header['filename'] = $p_filename; $p_header['stored_filename'] = $v_stored_filename; $p_header['extra'] = ''; $p_header['comment'] = ''; $p_header['status'] = 'ok'; $p_header['index'] = -1;
// ----- Look for pre-add callback if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
// ----- Generate a local information $v_local_header = array(); $this->privConvertHeader2FileInfo($p_header, $v_local_header);
// header. eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);'); if ($v_result == 0) { // ----- Change the file status $p_header['status'] = "skipped"; $v_result = 1; }
// ----- Update the informations if ($p_header['stored_filename'] != $v_local_header['stored_filename']) { $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']); } }
// ----- Look for empty stored filename if ($p_header['stored_filename'] == "") { $p_header['status'] = "filtered"; } // ----- Check the path length if (strlen($p_header['stored_filename']) > 0xFF) { $p_header['status'] = 'filename_too_long'; }
// ----- Look if no error, or file not skipped if ($p_header['status'] == 'ok') {
// ----- Look for a file if (is_file($p_filename)) { // ----- Open the source file if (($v_file = @fopen($p_filename, "rb")) == 0) { PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode"); return PclZip::errorCode(); }
if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) { // ----- Read the file content $v_content_compressed = @fread($v_file, $p_header['size']);
// ----- Calculate the CRC $p_header['crc'] = crc32($v_content_compressed); } else { // ----- Read the file content $v_content = @fread($v_file, $p_header['size']);
// ----- Calculate the CRC $p_header['crc'] = crc32($v_content);
// ----- Compress the file $v_content_compressed = gzdeflate($v_content); }
// ----- Set header parameters $p_header['compressed_size'] = strlen($v_content_compressed); $p_header['compression'] = 8;
// ----- Call the header generation if (($v_result = $this->privWriteFileHeader($p_header)) != 1) { @fclose($v_file); return $v_result; }
// ----- Write the compressed content $v_binary_data = pack('a'.$p_header['compressed_size'], $v_content_compressed); @fwrite($this->zip_fd, $v_binary_data, $p_header['compressed_size']); // ----- Close the file @fclose($v_file); }
// ----- Look for a directory else { // ----- Set the file properties $p_header['filename'] .= '/'; $p_header['filename_len']++; $p_header['size'] = 0; $p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
// ----- Call the header generation if (($v_result = $this->privWriteFileHeader($p_header)) != 1) { return $v_result; } } }
// ----- Look for pre-add callback if (isset($p_options[PCLZIP_CB_POST_ADD])) {
// ----- Generate a local information $v_local_header = array(); $this->privConvertHeader2FileInfo($p_header, $v_local_header);
// ----- Call the callback eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);'); if ($v_result == 0) { // ----- Ignored $v_result = 1; }
}
// ----- Return return $v_result; } // ------------------------------------ // Function : privList() // Description : // Parameters : // Return Values : // ------------------------------------ function privList(&$p_list) { $v_result=1;
// ----- Open the zip file if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
// ----- Return return PclZip::errorCode(); }
// ----- Read the central directory informations $v_central_dir = array(); if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) { return $v_result; }
// ----- Go to beginning of Central Dir @rewind($this->zip_fd); if (@fseek($this->zip_fd, $v_central_dir['offset'])) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
// ----- Return return PclZip::errorCode(); }
// ----- Read each entry for ($i=0; $i<$v_central_dir['entries']; $i++) { // ----- Read the file header if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) { //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } $v_header['index'] = $i;
// ----- Get the only interesting attributes $this->privConvertHeader2FileInfo($v_header, $p_list[$i]); unset($v_header); }
// ----- Close the zip file $this->privCloseFd();
// ----- Return return $v_result; }
// ------------------------------------ // Function : privConvertHeader2FileInfo() function privConvertHeader2FileInfo($p_header, &$p_info) { $v_result=1;
// ----- Get the interesting attributes $p_info['filename'] = $p_header['filename']; $p_info['stored_filename'] = $p_header['stored_filename']; $p_info['size'] = $p_header['size']; $p_info['compressed_size'] = $p_header['compressed_size']; $p_info['mtime'] = $p_header['mtime']; $p_info['comment'] = $p_header['comment']; $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010); $p_info['index'] = $p_header['index']; $p_info['status'] = $p_header['status'];
return $v_result; }
// ------------------------------------ // Function : privExtractByRule() function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options) { $v_result=1;
// ----- Check the path if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../") && (substr($p_path,1,2)!=":/"))) $p_path = "./".$p_path;
// ----- Reduce the path last (and duplicated) '/' if (($p_path != "./") && ($p_path != "/")) { // ----- Look for the path end '/' while (substr($p_path, -1) == "/") { $p_path = substr($p_path, 0, strlen($p_path)-1); } }
// ----- Look for path to remove format (should end by /) if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/')) { $p_remove_path .= '/'; } $p_remove_path_size = strlen($p_remove_path);
// ----- Open the zip file if (($v_result = $this->privOpenFd('rb')) != 1) { return $v_result; }
// ----- Read the central directory informations $v_central_dir = array(); if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) { // ----- Close the zip file $this->privCloseFd();
return $v_result; }
// ----- Start at beginning of Central Dir $v_pos_entry = $v_central_dir['offset'];
// ----- Read each entry $j_start = 0; for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++) {
// ----- Read next Central dir entry @rewind($this->zip_fd); if (@fseek($this->zip_fd, $v_pos_entry)) { // ----- Close the zip file $this->privCloseFd();
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
// ----- Return return PclZip::errorCode(); }
// ----- Read the file header $v_header = array(); if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) { // ----- Close the zip file $this->privCloseFd();
return $v_result; }
// ----- Store the index $v_header['index'] = $i;
// ----- Store the file position $v_pos_entry = ftell($this->zip_fd);
// ----- Look for the specific extract rules $v_extract = false;
// ----- Look for extract by name rule if ( (isset($p_options[PCLZIP_OPT_BY_NAME])) && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
// ----- Look if the filename is in the list for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
// ----- Look for a directory if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
// ----- Look if the directory is in the filename path if ( (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) { $v_extract = true; } } // ----- Look for a filename elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) { $v_extract = true; } } }
// ----- Look for extract by ereg rule else if ( (isset($p_options[PCLZIP_OPT_BY_EREG])) && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) { $v_extract = true; } }
// ----- Look for extract by preg rule else if ( (isset($p_options[PCLZIP_OPT_BY_PREG])) && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) { $v_extract = true; } }
// ----- Look for extract by index rule else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX])) && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) { // ----- Look if the index is in the list for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) { $v_extract = true; } if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) { $j_start = $j+1; }
if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) { break; } } }
// ----- Look for no rule, which means extract all the archive else { $v_extract = true; }
// ----- Look for real extraction if ($v_extract) {
// ----- Go to the file position @rewind($this->zip_fd); if (@fseek($this->zip_fd, $v_header['offset'])) { // ----- Close the zip file $this->privCloseFd();
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
// ----- Return return PclZip::errorCode(); }
// ----- Look for extraction as string if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
// ----- Extracting the file $v_result1 = $this->privExtractFileAsString($v_header, $v_string); if ($v_result1 < 1) { $this->privCloseFd(); return $v_result1; }
// ----- Get the only interesting attributes if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1) { // ----- Close the zip file $this->privCloseFd();
return $v_result; }
// ----- Set the file content $p_file_list[$v_nb_extracted]['content'] = $v_string;
// ----- Next extracted file $v_nb_extracted++; // ----- Look for user callback abort if ($v_result1 == 2) { break; } } // ----- Look for extraction in standard output elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) { // ----- Extracting the file in standard output $v_result1 = $this->privExtractFileInOutput($v_header, $p_options); if ($v_result1 < 1) { $this->privCloseFd(); return $v_result1; }
// ----- Get the only interesting attributes if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) { $this->privCloseFd(); return $v_result; }
// ----- Look for user callback abort if ($v_result1 == 2) { break; } } // ----- Look for normal extraction else { // ----- Extracting the file $v_result1 = $this->privExtractFile($v_header, $p_path, $p_remove_path, $p_remove_all_path, $p_options); if ($v_result1 < 1) { $this->privCloseFd(); return $v_result1; }
// ----- Get the only interesting attributes if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) { // ----- Close the zip file $this->privCloseFd();
return $v_result; }
// ----- Look for user callback abort if ($v_result1 == 2) { break; } } } }
// ----- Close the zip file $this->privCloseFd();
// ----- Return //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } // ------------------------------------
// ------------------------------------ // Function : privExtractFile() // Description : // Parameters : // Return Values : // ------------------------------------ function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options) { $v_result=1;
// ----- Read the file header if (($v_result = $this->privReadFileHeader($v_header)) != 1) { // ----- Return return $v_result; }
// ----- Look for all path to remove if ($p_remove_all_path == true) { // ----- Get the basename of the path $p_entry['filename'] = basename($p_entry['filename']); }
// ----- Look for path to remove else if ($p_remove_path != "") { if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2) {
// ----- Change the file status $p_entry['status'] = "filtered";
return $v_result; }
$p_remove_path_size = strlen($p_remove_path); if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
// ----- Remove the path $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
} }
// ----- Add the path if ($p_path != '') { $p_entry['filename'] = $p_path."/".$p_entry['filename']; }
// ----- Look for pre-extract callback if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
// ----- Generate a local information $v_local_header = array(); $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
// ----- Call the callback // header. eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);'); if ($v_result == 0) { // ----- Change the file status $p_entry['status'] = "skipped"; $v_result = 1; } // ----- Look for abort result if ($v_result == 2) { // ----- This status is internal and will be changed in 'skipped' $p_entry['status'] = "aborted"; $v_result = PCLZIP_ERR_USER_ABORTED; }
$p_entry['filename'] = $v_local_header['filename']; }
// ----- Look if extraction should be done if ($p_entry['status'] == 'ok') {
// ----- Look for specific actions while the file exist if (file_exists($p_entry['filename'])) {
// ----- Look if file is a directory if (is_dir($p_entry['filename'])) {
// ----- Change the file status $p_entry['status'] = "already_a_directory";
} // ----- Look if file is write protected else if (!is_writeable($p_entry['filename'])) {
// ----- Change the file status $p_entry['status'] = "write_protected";
}
// ----- Look if the extracted file is older else if (filemtime($p_entry['filename']) > $p_entry['mtime']) {
// ----- Change the file status $p_entry['status'] = "newer_exist";
} }
// ----- Check the directory availability and create it if necessary else { if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/')) $v_dir_to_check = $p_entry['filename']; else if (!strstr($p_entry['filename'], "/")) $v_dir_to_check = ""; else $v_dir_to_check = dirname($p_entry['filename']);
if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
// ----- Change the file status $p_entry['status'] = "path_creation_fail";
$v_result = 1; } } }
// ----- Look if extraction should be done if ($p_entry['status'] == 'ok') {
// ----- Do the extraction (if not a folder) if (!(($p_entry['external']&0x00000010)==0x00000010)) {
// ----- Look for not compressed file if ($p_entry['compressed_size'] == $p_entry['size']) {
// ----- Opening destination file if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
// ----- Change the file status $p_entry['status'] = "write_error";
// ----- Return return $v_result; }
// ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks $v_size = $p_entry['compressed_size']; while ($v_size != 0) { $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE); $v_buffer = fread($this->zip_fd, $v_read_size); $v_binary_data = pack('a'.$v_read_size, $v_buffer); @fwrite($v_dest_file, $v_binary_data, $v_read_size); $v_size -= $v_read_size; }
// ----- Closing the destination file fclose($v_dest_file);
// ----- Change the file mtime touch($p_entry['filename'], $p_entry['mtime']); } else { // ----- Trace
// ----- Opening destination file if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
// ----- Change the file status $p_entry['status'] = "write_error";
return $v_result; }
// ----- Read the compressed file in a buffer (one shot) $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
// ----- Decompress the file $v_file_content = gzinflate($v_buffer); unset($v_buffer);
// ----- Write the uncompressed data @fwrite($v_dest_file, $v_file_content, $p_entry['size']); unset($v_file_content);
// ----- Closing the destination file @fclose($v_dest_file);
// ----- Change the file mtime touch($p_entry['filename'], $p_entry['mtime']); }
// ----- Look for chmod option if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
// ----- Change the mode of the file chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]); }
} }
// ----- Change abort status if ($p_entry['status'] == "aborted") { $p_entry['status'] = "skipped"; } // ----- Look for post-extract callback elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
// ----- Generate a local information $v_local_header = array(); $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
// ----- Call the callback eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
// ----- Look for abort result if ($v_result == 2) { $v_result = PCLZIP_ERR_USER_ABORTED; } }
// ----- Return return $v_result; }
// ------------------------------------ // Function : privExtractFileInOutput() // Description : // Parameters : // Return Values : // ------------------------------------ function privExtractFileInOutput(&$p_entry, &$p_options) { $v_result=1;
// ----- Read the file header if (($v_result = $this->privReadFileHeader($v_header)) != 1) { return $v_result; }
if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
// ----- Generate a local information $v_local_header = array(); $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
// ----- Call the callback // header. eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);'); if ($v_result == 0) { // ----- Change the file status $p_entry['status'] = "skipped"; $v_result = 1; }
// ----- Look for abort result if ($v_result == 2) { // ----- This status is internal and will be changed in 'skipped' $p_entry['status'] = "aborted"; $v_result = PCLZIP_ERR_USER_ABORTED; }
// ----- Update the informations $p_entry['filename'] = $v_local_header['filename']; }
// ----- Look if extraction should be done if ($p_entry['status'] == 'ok') {
// ----- Do the extraction (if not a folder) if (!(($p_entry['external']&0x00000010)==0x00000010)) { // ----- Look for not compressed file if ($p_entry['compressed_size'] == $p_entry['size']) {
// ----- Read the file in a buffer (one shot) $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
// ----- Send the file to the output echo $v_buffer; unset($v_buffer); } else { // ----- Read the compressed file in a buffer (one shot) $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); // ----- Decompress the file $v_file_content = gzinflate($v_buffer); unset($v_buffer);
// ----- Send the file to the output echo $v_file_content; unset($v_file_content); } } }
// ----- Change abort status if ($p_entry['status'] == "aborted") { $p_entry['status'] = "skipped"; }
// ----- Look for post-extract callback elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
// ----- Generate a local information $v_local_header = array(); $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
// ----- Call the callback eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
// ----- Look for abort result if ($v_result == 2) { $v_result = PCLZIP_ERR_USER_ABORTED; } }
return $v_result; } // ------------------------------------
// ------------------------------------ // Function : privExtractFileAsString() // Description : // Parameters : // Return Values : // ------------------------------------ function privExtractFileAsString(&$p_entry, &$p_string) { $v_result=1;
// ----- Read the file header $v_header = array(); if (($v_result = $this->privReadFileHeader($v_header)) != 1) { // ----- Return return $v_result; }
if (!(($p_entry['external']&0x00000010)==0x00000010)) { if ($p_entry['compressed_size'] == $p_entry['size']) {
// ----- Reading the file $p_string = fread($this->zip_fd, $p_entry['compressed_size']); } else {
// ----- Reading the file $v_data = fread($this->zip_fd, $p_entry['compressed_size']); // ----- Decompress the file $p_string = gzinflate($v_data); }
} else { }
return $v_result; }
// ------------------------------------ // Function : privReadFileHeader() // Description : // Parameters : // Return Values : // ------------------------------------ function privReadFileHeader(&$p_header) { $v_result=1;
// ----- Read the 4 bytes signature $v_binary_data = @fread($this->zip_fd, 4); $v_data = unpack('Vid', $v_binary_data);
// ----- Check signature if ($v_data['id'] != 0x04034b50) {
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
// ----- Return return PclZip::errorCode(); }
// ----- Read the first 42 bytes of the header $v_binary_data = fread($this->zip_fd, 26);
// ----- Look for invalid block size if (strlen($v_binary_data) != 26) { $p_header['filename'] = ""; $p_header['status'] = "invalid_header";
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
// ----- Return return PclZip::errorCode(); }
// ----- Extract the values $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
// ----- Get filename $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
// ----- Get extra_fields if ($v_data['extra_len'] != 0) { $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']); } else { $p_header['extra'] = ''; }
// ----- Extract properties $p_header['compression'] = $v_data['compression']; $p_header['size'] = $v_data['size']; $p_header['compressed_size'] = $v_data['compressed_size']; $p_header['crc'] = $v_data['crc']; $p_header['flag'] = $v_data['flag'];
// ----- Recuperate date in UNIX format $p_header['mdate'] = $v_data['mdate']; $p_header['mtime'] = $v_data['mtime']; if ($p_header['mdate'] && $p_header['mtime']) { // ----- Extract time $v_hour = ($p_header['mtime'] & 0xF800) >> 11; $v_minute = ($p_header['mtime'] & 0x07E0) >> 5; $v_seconde = ($p_header['mtime'] & 0x001F)*2;
// ----- Extract date $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980; $v_month = ($p_header['mdate'] & 0x01E0) >> 5; $v_day = $p_header['mdate'] & 0x001F;
// ----- Get UNIX date format $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
} else { $p_header['mtime'] = time(); }
$p_header['stored_filename'] = $p_header['filename'];
// ----- Set the status field $p_header['status'] = "ok";
// ----- Return return $v_result; } // ------------------------------------
// ------------------------------------ // Function : privReadCentralFileHeader() function privReadCentralFileHeader(&$p_header) { $v_result=1;
// ----- Read the 4 bytes signature $v_binary_data = @fread($this->zip_fd, 4); $v_data = unpack('Vid', $v_binary_data);
// ----- Check signature if ($v_data['id'] != 0x02014b50) {
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
// ----- Return return PclZip::errorCode(); }
// ----- Read the first 42 bytes of the header $v_binary_data = fread($this->zip_fd, 42);
// ----- Look for invalid block size if (strlen($v_binary_data) != 42) { $p_header['filename'] = ""; $p_header['status'] = "invalid_header";
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
return PclZip::errorCode(); }
$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);
// ----- Get filename if ($p_header['filename_len'] != 0) $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']); else $p_header['filename'] = '';
// ----- Get extra if ($p_header['extra_len'] != 0) $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']); else $p_header['extra'] = '';
// ----- Get comment if ($p_header['comment_len'] != 0) $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']); else $p_header['comment'] = '';
// ----- Recuperate date in UNIX format if ($p_header['mdate'] && $p_header['mtime']) { // ----- Extract time $v_hour = ($p_header['mtime'] & 0xF800) >> 11; $v_minute = ($p_header['mtime'] & 0x07E0) >> 5; $v_seconde = ($p_header['mtime'] & 0x001F)*2;
// ----- Extract date $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980; $v_month = ($p_header['mdate'] & 0x01E0) >> 5; $v_day = $p_header['mdate'] & 0x001F;
// ----- Get UNIX date format $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
} else { $p_header['mtime'] = time(); }
// ----- Set the stored filename $p_header['stored_filename'] = $p_header['filename'];
// ----- Set default status to ok $p_header['status'] = 'ok';
if (substr($p_header['filename'], -1) == '/') { $p_header['external'] = 0x41FF0010; }
return $v_result; }
// ------------------------------------ // Function : privReadEndCentralDir() function privReadEndCentralDir(&$p_central_dir) { $v_result=1;
// ----- Go to the end of the zip file $v_size = filesize($this->zipname); @fseek($this->zip_fd, $v_size); if (@ftell($this->zip_fd) != $v_size) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
return PclZip::errorCode(); }
// in this case the end of central dir is at 22 bytes of the file end $v_found = 0; if ($v_size > 26) { @fseek($this->zip_fd, $v_size-22); if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22)) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
// ----- Return return PclZip::errorCode(); }
$v_binary_data = @fread($this->zip_fd, 4); $v_data = @unpack('Vid', $v_binary_data);
// ----- Check signature if ($v_data['id'] == 0x06054b50) { $v_found = 1; }
$v_pos = ftell($this->zip_fd); }
// ----- Go back to the maximum possible size of the Central Dir End Record if (!$v_found) { $v_maximum_size = 65557; // 0xFFFF + 22; if ($v_maximum_size > $v_size) $v_maximum_size = $v_size; @fseek($this->zip_fd, $v_size-$v_maximum_size); if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size)) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
// ----- Return return PclZip::errorCode(); }
// ----- Read byte per byte in order to find the signature $v_pos = ftell($this->zip_fd); $v_bytes = 0x00000000; while ($v_pos < $v_size) { // ----- Read a byte $v_byte = @fread($this->zip_fd, 1);
// ----- Add the byte $v_bytes = ($v_bytes << 8) | Ord($v_byte);
// ----- Compare the bytes if ($v_bytes == 0x504b0506) { $v_pos++; break; }
$v_pos++; }
// ----- Look if not found end of central dir if ($v_pos == $v_size) {
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
return PclZip::errorCode(); } }
// ----- Read the first 18 bytes of the header $v_binary_data = fread($this->zip_fd, 18);
// ----- Look for invalid block size if (strlen($v_binary_data) != 18) {
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
// ----- Return return PclZip::errorCode(); }
$v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
// ----- Error log PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Fail to find the right signature");
return PclZip::errorCode(); }
// ----- Get comment if ($v_data['comment_size'] != 0) $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']); else $p_central_dir['comment'] = '';
$p_central_dir['entries'] = $v_data['entries']; $p_central_dir['disk_entries'] = $v_data['disk_entries']; $p_central_dir['offset'] = $v_data['offset']; $p_central_dir['size'] = $v_data['size']; $p_central_dir['disk'] = $v_data['disk']; $p_central_dir['disk_start'] = $v_data['disk_start'];
return $v_result; }
// ------------------------------------ // Function : privDirCheck() function privDirCheck($p_dir, $p_is_dir=false) { $v_result = 1;
// ----- Remove the final '/' if (($p_is_dir) && (substr($p_dir, -1)=='/')) { $p_dir = substr($p_dir, 0, strlen($p_dir)-1); }
// ----- Check the directory availability if ((is_dir($p_dir)) || ($p_dir == "")) { return 1; }
// ----- Extract parent directory $p_parent_dir = dirname($p_dir);
// ----- Just a check if ($p_parent_dir != $p_dir) { // ----- Look for parent directory if ($p_parent_dir != "") { if (($v_result = $this->privDirCheck($p_parent_dir)) != 1) { return $v_result; } } }
if (!@mkdir($p_dir, 0777)) { // ----- Error log PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
// ----- Return return PclZip::errorCode(); }
// ----- Return return $v_result; } // ------------------------------------ // Function : privDuplicate() // Description : // Parameters : // Return Values : // ------------------------------------ function privDuplicate($p_archive_filename) { $v_result=1;
// ----- Look if the $p_archive_filename exists if (!is_file($p_archive_filename)) {
// ----- Nothing to duplicate, so duplicate is a success. $v_result = 1;
// ----- Return return $v_result; } if (($v_result=$this->privOpenFd('wb')) != 1) { return $v_result; } if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) { $this->privCloseFd();
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode'); return PclZip::errorCode(); }
$v_size = filesize($p_archive_filename); while ($v_size != 0) { $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE); $v_buffer = fread($v_zip_temp_fd, $v_read_size); @fwrite($this->zip_fd, $v_buffer, $v_read_size); $v_size -= $v_read_size; }
// ----- Close $this->privCloseFd();
// ----- Close the temporary file @fclose($v_zip_temp_fd);
return $v_result; }
// ------------------------------------ // Function : privErrorLog() // Description : // Parameters : // ------------------------------------ function privErrorLog($p_error_code=0, $p_error_string='') { if (PCLZIP_ERROR_EXTERNAL == 1) { PclError($p_error_code, $p_error_string); } else { $this->error_code = $p_error_code; $this->error_string = $p_error_string; } } // ------------------------------------
// ------------------------------------ // Function : privErrorReset() // Description : // Parameters : // ------------------------------------ function privErrorReset() { if (PCLZIP_ERROR_EXTERNAL == 1) { PclErrorReset(); } else { $this->error_code = 1; $this->error_string = ''; } } // ------------------------------------
} // End of class // ------------------------------------
// ------------------------------------ // Function : PclZipUtilPathReduction() function PclZipUtilPathReduction($p_dir) { $v_result = "";
// ----- Look for not empty path if ($p_dir != "") { // ----- Explode path by directory names $v_list = explode("/", $p_dir);
// ----- Study directories from last to first for ($i=sizeof($v_list)-1; $i>=0; $i--) { // ----- Look for current path if ($v_list[$i] == ".") { } else if ($v_list[$i] == "..") { // ----- Ignore it and ignore the $i-1 $i--; } else if (($v_list[$i] == "") && ($i!=(sizeof($v_list)-1)) && ($i!=0)) { } else { $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:""); } } }
// ----- Return return $v_result; } // ------------------------------------
// ------------------------------------ // Function : PclZipUtilPathInclusion() function PclZipUtilPathInclusion($p_dir, $p_path) { $v_result = 1;
// ----- Explode dir and path by directory separator $v_list_dir = explode("/", $p_dir); $v_list_dir_size = sizeof($v_list_dir); $v_list_path = explode("/", $p_path); $v_list_path_size = sizeof($v_list_path);
// ----- Study directories paths $i = 0; $j = 0; while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
// ----- Look for empty dir (path reduction) if ($v_list_dir[$i] == '') { $i++; continue; } if ($v_list_path[$j] == '') { $j++; continue; }
// ----- Compare the items if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) { $v_result = 0; }
// ----- Next items $i++; $j++; }
// ----- Look if everything seems to be the same if ($v_result) { // ----- Skip all the empty items while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++; while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) { // ----- There are exactly the same $v_result = 2; } else if ($i < $v_list_dir_size) { // ----- The path is shorter than the dir $v_result = 0; } }
// ----- Return return $v_result; } // ------------------------------------ // Function : PclZipUtilOptionText() function PclZipUtilOptionText($p_option) {
switch ($p_option) { case PCLZIP_OPT_PATH : $v_result = 'PCLZIP_OPT_PATH'; break; case PCLZIP_OPT_ADD_PATH : $v_result = 'PCLZIP_OPT_ADD_PATH'; break; case PCLZIP_OPT_REMOVE_PATH : $v_result = 'PCLZIP_OPT_REMOVE_PATH'; break; case PCLZIP_OPT_REMOVE_ALL_PATH : $v_result = 'PCLZIP_OPT_REMOVE_ALL_PATH'; break; case PCLZIP_OPT_EXTRACT_AS_STRING : $v_result = 'PCLZIP_OPT_EXTRACT_AS_STRING'; break; case PCLZIP_OPT_SET_CHMOD : $v_result = 'PCLZIP_OPT_SET_CHMOD'; break; case PCLZIP_OPT_BY_NAME : $v_result = 'PCLZIP_OPT_BY_NAME'; break; case PCLZIP_OPT_BY_INDEX : $v_result = 'PCLZIP_OPT_BY_INDEX'; break; case PCLZIP_OPT_BY_EREG : $v_result = 'PCLZIP_OPT_BY_EREG'; break; case PCLZIP_OPT_BY_PREG : $v_result = 'PCLZIP_OPT_BY_PREG'; break;
case PCLZIP_CB_PRE_EXTRACT : $v_result = 'PCLZIP_CB_PRE_EXTRACT'; break; case PCLZIP_CB_POST_EXTRACT : $v_result = 'PCLZIP_CB_POST_EXTRACT'; break; case PCLZIP_CB_PRE_ADD : $v_result = 'PCLZIP_CB_PRE_ADD'; break; case PCLZIP_CB_POST_ADD : $v_result = 'PCLZIP_CB_POST_ADD'; break;
default : $v_result = 'Unknown'; }
// ----- Return return $v_result; }
// ------------------------------------ // Function : PclZipUtilTranslateWinPath() function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true) { if (stristr(php_uname(), 'windows')) { // ----- Look for potential disk letter if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) { $p_path = substr($p_path, $v_position+1); } // ----- Change potential windows directory separator if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) { $p_path = strtr($p_path, '\\', '/'); } } return $p_path; }
?>
|