You are here

function PclZip::privAdd in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/lib/pclzip/pclzip.lib.php \PclZip::privAdd()
1 call to PclZip::privAdd()
PclZip::add in includes/moodle/lib/pclzip/pclzip.lib.php

File

includes/moodle/lib/pclzip/pclzip.lib.php, line 2103

Class

PclZip

Code

function privAdd($p_filedescr_list, &$p_result_list, &$p_options) {

  //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAdd", "list");
  $v_result = 1;
  $v_list_detail = array();

  // ----- Look if the archive exists or is empty
  if (!is_file($this->zipname) || filesize($this->zipname) == 0) {

    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, or is empty, create it.");

    // ----- Do a create
    $v_result = $this
      ->privCreate($p_filedescr_list, $p_result_list, $p_options);

    // ----- Return

    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
    return $v_result;
  }

  // ----- Magic quotes trick
  $this
    ->privDisableMagicQuotes();

  // ----- Open the zip file

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  if (($v_result = $this
    ->privOpenFd('rb')) != 1) {

    // ----- Magic quotes trick
    $this
      ->privSwapBackMagicQuotes();

    // ----- Return

    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
    return $v_result;
  }

  // ----- Read the central directory informations
  $v_central_dir = array();
  if (($v_result = $this
    ->privReadEndCentralDir($v_central_dir)) != 1) {
    $this
      ->privCloseFd();
    $this
      ->privSwapBackMagicQuotes();

    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
    return $v_result;
  }

  // ----- Go to beginning of File

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
  @rewind($this->zip_fd);

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");

  // ----- Creates a temporay file
  $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';

  // ----- Open the temporary file in write mode

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
    $this
      ->privCloseFd();
    $this
      ->privSwapBackMagicQuotes();
    PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_zip_temp_name . '\' in binary write mode');

    // ----- Return

    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
    return PclZip::errorCode();
  }

  // ----- Copy the files from the archive to the temporary file
  // TBC : Here I should better append the file and go back to erase the central dir
  $v_size = $v_central_dir['offset'];
  while ($v_size != 0) {
    $v_read_size = $v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE;

    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
    $v_buffer = fread($this->zip_fd, $v_read_size);
    @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
    $v_size -= $v_read_size;
  }

  // ----- Swap the file descriptor
  // Here is a trick : I swap the temporary fd with the zip fd, in order to use
  // the following methods on the temporary fil and not the real archive
  $v_swap = $this->zip_fd;
  $this->zip_fd = $v_zip_temp_fd;
  $v_zip_temp_fd = $v_swap;

  // ----- Add the files
  $v_header_list = array();
  if (($v_result = $this
    ->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
    fclose($v_zip_temp_fd);
    $this
      ->privCloseFd();
    @unlink($v_zip_temp_name);
    $this
      ->privSwapBackMagicQuotes();

    // ----- Return

    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
    return $v_result;
  }

  // ----- Store the offset of the central dir
  $v_offset = @ftell($this->zip_fd);

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");

  // ----- Copy the block of file headers from the old archive
  $v_size = $v_central_dir['size'];
  while ($v_size != 0) {
    $v_read_size = $v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE;

    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
    $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
    @fwrite($this->zip_fd, $v_buffer, $v_read_size);
    $v_size -= $v_read_size;
  }

  // ----- Create the Central Dir files header
  for ($i = 0, $v_count = 0; $i < sizeof($v_header_list); $i++) {

    // ----- Create the file header
    if ($v_header_list[$i]['status'] == 'ok') {
      if (($v_result = $this
        ->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
        fclose($v_zip_temp_fd);
        $this
          ->privCloseFd();
        @unlink($v_zip_temp_name);
        $this
          ->privSwapBackMagicQuotes();

        // ----- Return

        //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
        return $v_result;
      }
      $v_count++;
    }

    // ----- Transform the header to a 'usable' info
    $this
      ->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  }

  // ----- Zip file comment
  $v_comment = $v_central_dir['comment'];
  if (isset($p_options[PCLZIP_OPT_COMMENT])) {
    $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  }
  if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
    $v_comment = $v_comment . $p_options[PCLZIP_OPT_ADD_COMMENT];
  }
  if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
    $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT] . $v_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_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1) {

    // ----- Reset the file list
    unset($v_header_list);
    $this
      ->privSwapBackMagicQuotes();

    // ----- Return

    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
    return $v_result;
  }

  // ----- Swap back the file descriptor
  $v_swap = $this->zip_fd;
  $this->zip_fd = $v_zip_temp_fd;
  $v_zip_temp_fd = $v_swap;

  // ----- Close
  $this
    ->privCloseFd();

  // ----- Close the temporary file
  @fclose($v_zip_temp_fd);

  // ----- Magic quotes trick
  $this
    ->privSwapBackMagicQuotes();

  // ----- 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);
  PclZipUtilRename($v_zip_temp_name, $this->zipname);

  // ----- Return

  //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
  return $v_result;
}