예제 #1
0
파일: Zip.php 프로젝트: alexzita/alex_blog
 /**
  * Archive_Zip::_deleteByRule()
  *
  * { Description }
  *
  */
 function _deleteByRule(&$p_result_list, &$p_params)
 {
     $v_result = 1;
     $v_list_detail = array();
     // ----- Open the zip file
     if (($v_result = $this->_openFd('rb')) != 1) {
         // ----- Return
         return $v_result;
     }
     // ----- Read the central directory informations
     $v_central_dir = array();
     if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
         $this->_closeFd();
         return $v_result;
     }
     // ----- Go to beginning of File
     @rewind($this->_zip_fd);
     // ----- Scan all the files
     // ----- Start at beginning of Central Dir
     $v_pos_entry = $v_central_dir['offset'];
     @rewind($this->_zip_fd);
     if (@fseek($this->_zip_fd, $v_pos_entry)) {
         // ----- Clean
         $this->_closeFd();
         $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
         return Archive_Zip::errorCode();
     }
     // ----- Read each entry
     $v_header_list = array();
     $j_start = 0;
     for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; $i++) {
         // ----- Read the file header
         $v_header_list[$v_nb_extracted] = array();
         $v_result = $this->_readCentralFileHeader($v_header_list[$v_nb_extracted]);
         if ($v_result != 1) {
             // ----- Clean
             $this->_closeFd();
             return $v_result;
         }
         // ----- Store the index
         $v_header_list[$v_nb_extracted]['index'] = $i;
         // ----- Look for the specific extract rules
         $v_found = false;
         // ----- Look for extract by name rule
         if (isset($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]) && $p_params[ARCHIVE_ZIP_PARAM_BY_NAME] != 0) {
             // ----- Look if the filename is in the list
             for ($j = 0; $j < sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]) && !$v_found; $j++) {
                 // ----- Look for a directory
                 if (substr($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j], -1) == "/") {
                     // ----- Look if the directory is in the filename path
                     if (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) && substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) {
                         $v_found = true;
                     } elseif (($v_header_list[$v_nb_extracted]['external'] & 0x10) == 0x10 && $v_header_list[$v_nb_extracted]['stored_filename'] . '/' == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) {
                         $v_found = true;
                     }
                 } elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) {
                     $v_found = true;
                 }
             }
         } else {
             if (isset($p_params[ARCHIVE_ZIP_PARAM_BY_EREG]) && $p_params[ARCHIVE_ZIP_PARAM_BY_EREG] != "") {
                 if (ereg($p_params[ARCHIVE_ZIP_PARAM_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
                     $v_found = true;
                 }
             } else {
                 if (isset($p_params[ARCHIVE_ZIP_PARAM_BY_PREG]) && $p_params[ARCHIVE_ZIP_PARAM_BY_PREG] != "") {
                     if (preg_match($p_params[ARCHIVE_ZIP_PARAM_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
                         $v_found = true;
                     }
                 } else {
                     if (isset($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]) && $p_params[ARCHIVE_ZIP_PARAM_BY_INDEX] != 0) {
                         // ----- Look if the index is in the list
                         for ($j = $j_start; $j < sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]) && !$v_found; $j++) {
                             if ($i >= $p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start'] && $i <= $p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end']) {
                                 $v_found = true;
                             }
                             if ($i >= $p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end']) {
                                 $j_start = $j + 1;
                             }
                             if ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start'] > $i) {
                                 break;
                             }
                         }
                     }
                 }
             }
         }
         // ----- Look for deletion
         if ($v_found) {
             unset($v_header_list[$v_nb_extracted]);
         } else {
             $v_nb_extracted++;
         }
     }
     // ----- Look if something need to be deleted
     if ($v_nb_extracted > 0) {
         // ----- Creates a temporay file
         $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR . uniqid('archive_zip-') . '.tmp';
         // ----- Creates a temporary zip archive
         $v_temp_zip = new Archive_Zip($v_zip_temp_name);
         // ----- Open the temporary zip file in write mode
         if (($v_result = $v_temp_zip->_openFd('wb')) != 1) {
             $this->_closeFd();
             // ----- Return
             return $v_result;
         }
         // ----- Look which file need to be kept
         for ($i = 0; $i < sizeof($v_header_list); $i++) {
             // ----- Calculate the position of the header
             @rewind($this->_zip_fd);
             if (@fseek($this->_zip_fd, $v_header_list[$i]['offset'])) {
                 // ----- Clean
                 $this->_closeFd();
                 $v_temp_zip->_closeFd();
                 @unlink($v_zip_temp_name);
                 $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
                 return Archive_Zip::errorCode();
             }
             // ----- Read the file header
             if (($v_result = $this->_readFileHeader($v_header_list[$i])) != 1) {
                 // ----- Clean
                 $this->_closeFd();
                 $v_temp_zip->_closeFd();
                 @unlink($v_zip_temp_name);
                 return $v_result;
             }
             // ----- Write the file header
             $v_result = $v_temp_zip->_writeFileHeader($v_header_list[$i]);
             if ($v_result != 1) {
                 // ----- Clean
                 $this->_closeFd();
                 $v_temp_zip->_closeFd();
                 @unlink($v_zip_temp_name);
                 return $v_result;
             }
             // ----- Read/write the data block
             $v_result = $this->_tool_CopyBlock($this->_zip_fd, $v_temp_zip->_zip_fd, $v_header_list[$i]['compressed_size']);
             if ($v_result != 1) {
                 // ----- Clean
                 $this->_closeFd();
                 $v_temp_zip->_closeFd();
                 @unlink($v_zip_temp_name);
                 return $v_result;
             }
         }
         // ----- Store the offset of the central dir
         $v_offset = @ftell($v_temp_zip->_zip_fd);
         // ----- Re-Create the Central Dir files header
         for ($i = 0; $i < sizeof($v_header_list); $i++) {
             // ----- Create the file header
             $v_result = $v_temp_zip->_writeCentralFileHeader($v_header_list[$i]);
             if ($v_result != 1) {
                 // ----- Clean
                 $v_temp_zip->_closeFd();
                 $this->_closeFd();
                 @unlink($v_zip_temp_name);
                 return $v_result;
             }
             // ----- Transform the header to a 'usable' info
             $v_temp_zip->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
         }
         // ----- Zip file comment
         $v_comment = '';
         // ----- Calculate the size of the central header
         $v_size = @ftell($v_temp_zip->_zip_fd) - $v_offset;
         // ----- Create the central dir footer
         $v_result = $v_temp_zip->_writeCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment);
         if ($v_result != 1) {
             // ----- Clean
             unset($v_header_list);
             $v_temp_zip->_closeFd();
             $this->_closeFd();
             @unlink($v_zip_temp_name);
             return $v_result;
         }
         // ----- Close
         $v_temp_zip->_closeFd();
         $this->_closeFd();
         // ----- Delete the zip file
         // TBC : I should test the result ...
         @unlink($this->_zipname);
         // ----- Rename the temporary file
         // TBC : I should test the result ...
         //@rename($v_zip_temp_name, $this->_zipname);
         $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
         // ----- Destroy the temporary archive
         unset($v_temp_zip);
     }
     // ----- Return
     return $v_result;
 }