You are here

public function ArchiveTar::_extractList in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Archiver/ArchiveTar.php \Drupal\Core\Archiver\ArchiveTar::_extractList()

Parameters

string $p_path:

string $p_list_detail:

string $p_mode:

string $p_file_list:

string $p_remove_path:

bool $p_preserve:

Return value

bool

3 calls to ArchiveTar::_extractList()
ArchiveTar::extractList in core/lib/Drupal/Core/Archiver/ArchiveTar.php
This method extract from the archive only the files indicated in the $p_filelist. These files are extracted in the current directory or in the directory indicated by the optional $p_path parameter. If indicated the $p_remove_path can be used in the…
ArchiveTar::extractModify in core/lib/Drupal/Core/Archiver/ArchiveTar.php
This method extract all the content of the archive in the directory indicated by $p_path. When relevant the memorized path of the files/dir can be modified by removing the $p_remove_path path at the beginning of the file/dir path. While extracting a…
ArchiveTar::listContent in core/lib/Drupal/Core/Archiver/ArchiveTar.php

File

core/lib/Drupal/Core/Archiver/ArchiveTar.php, line 1919

Class

ArchiveTar

Namespace

Drupal\Core\Archiver

Code

public function _extractList($p_path, &$p_list_detail, $p_mode, $p_file_list, $p_remove_path, $p_preserve = false) {
  $v_result = true;
  $v_nb = 0;
  $v_extract_all = true;
  $v_listing = false;
  $p_path = $this
    ->_translateWinPath($p_path, false);
  if ($p_path == '' || substr($p_path, 0, 1) != '/' && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':')) {
    $p_path = "./" . $p_path;
  }
  $p_remove_path = $this
    ->_translateWinPath($p_remove_path);

  // ----- 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);
  switch ($p_mode) {
    case "complete":
      $v_extract_all = true;
      $v_listing = false;
      break;
    case "partial":
      $v_extract_all = false;
      $v_listing = false;
      break;
    case "list":
      $v_extract_all = false;
      $v_listing = true;
      break;
    default:
      $this
        ->_error('Invalid extract mode (' . $p_mode . ')');
      return false;
  }
  clearstatcache();
  while (strlen($v_binary_data = $this
    ->_readBlock()) != 0) {
    $v_extract_file = false;
    $v_extraction_stopped = 0;
    if (!$this
      ->_readHeader($v_binary_data, $v_header)) {
      return false;
    }
    if ($v_header['filename'] == '') {
      continue;
    }

    // ----- Look for long filename
    if ($v_header['typeflag'] == 'L') {
      if (!$this
        ->_readLongHeader($v_header)) {
        return false;
      }
    }

    // ignore extended / pax headers
    if ($v_header['typeflag'] == 'x' || $v_header['typeflag'] == 'g') {
      $this
        ->_jumpBlock(ceil($v_header['size'] / 512));
      continue;
    }
    if (!$v_extract_all && is_array($p_file_list)) {

      // ----- By default no unzip if the file is not found
      $v_extract_file = false;
      for ($i = 0; $i < sizeof($p_file_list); $i++) {

        // ----- Look if it is a directory
        if (substr($p_file_list[$i], -1) == '/') {

          // ----- Look if the directory is in the filename path
          if (strlen($v_header['filename']) > strlen($p_file_list[$i]) && substr($v_header['filename'], 0, strlen($p_file_list[$i])) == $p_file_list[$i]) {
            $v_extract_file = true;
            break;
          }
        }
        elseif ($p_file_list[$i] == $v_header['filename']) {
          $v_extract_file = true;
          break;
        }
      }
    }
    else {
      $v_extract_file = true;
    }

    // ----- Look if this file need to be extracted
    if ($v_extract_file && !$v_listing) {
      if ($p_remove_path != '' && substr($v_header['filename'] . '/', 0, $p_remove_path_size) == $p_remove_path) {
        $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
        if ($v_header['filename'] == '') {
          continue;
        }
      }
      if ($p_path != './' && $p_path != '/') {
        while (substr($p_path, -1) == '/') {
          $p_path = substr($p_path, 0, strlen($p_path) - 1);
        }
        if (substr($v_header['filename'], 0, 1) == '/') {
          $v_header['filename'] = $p_path . $v_header['filename'];
        }
        else {
          $v_header['filename'] = $p_path . '/' . $v_header['filename'];
        }
      }
      if (file_exists($v_header['filename'])) {
        if (@is_dir($v_header['filename']) && $v_header['typeflag'] == '') {
          $this
            ->_error('File ' . $v_header['filename'] . ' already exists as a directory');
          return false;
        }
        if ($this
          ->_isArchive($v_header['filename']) && $v_header['typeflag'] == "5") {
          $this
            ->_error('Directory ' . $v_header['filename'] . ' already exists as a file');
          return false;
        }
        if (!is_writeable($v_header['filename'])) {
          $this
            ->_error('File ' . $v_header['filename'] . ' already exists and is write protected');
          return false;
        }
        if (filemtime($v_header['filename']) > $v_header['mtime']) {

          // To be completed : An error or silent no replace ?
        }
      }
      elseif (($v_result = $this
        ->_dirCheck($v_header['typeflag'] == "5" ? $v_header['filename'] : dirname($v_header['filename']))) != 1) {
        $this
          ->_error('Unable to create path for ' . $v_header['filename']);
        return false;
      }
      if ($v_extract_file) {
        if ($v_header['typeflag'] == "5") {
          if (!@file_exists($v_header['filename'])) {
            if (!@mkdir($v_header['filename'], 0777)) {
              $this
                ->_error('Unable to create directory {' . $v_header['filename'] . '}');
              return false;
            }
          }
        }
        elseif ($v_header['typeflag'] == "2") {
          if (@file_exists($v_header['filename'])) {
            @drupal_unlink($v_header['filename']);
          }
          if (!@symlink($v_header['link'], $v_header['filename'])) {
            $this
              ->_error('Unable to extract symbolic link {' . $v_header['filename'] . '}');
            return false;
          }
        }
        else {
          if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
            $this
              ->_error('Error while opening {' . $v_header['filename'] . '} in write binary mode');
            return false;
          }
          else {
            $n = floor($v_header['size'] / 512);
            for ($i = 0; $i < $n; $i++) {
              $v_content = $this
                ->_readBlock();
              fwrite($v_dest_file, $v_content, 512);
            }
            if ($v_header['size'] % 512 != 0) {
              $v_content = $this
                ->_readBlock();
              fwrite($v_dest_file, $v_content, $v_header['size'] % 512);
            }
            @fclose($v_dest_file);
            if ($p_preserve) {
              @chown($v_header['filename'], $v_header['uid']);
              @chgrp($v_header['filename'], $v_header['gid']);
            }

            // ----- Change the file mode, mtime
            @touch($v_header['filename'], $v_header['mtime']);
            if ($v_header['mode'] & 0111) {

              // make file executable, obey umask
              $mode = fileperms($v_header['filename']) | ~umask() & 0111;
              @chmod($v_header['filename'], $mode);
            }
          }

          // ----- Check the file size
          clearstatcache();
          if (!is_file($v_header['filename'])) {
            $this
              ->_error('Extracted file ' . $v_header['filename'] . 'does not exist. Archive may be corrupted.');
            return false;
          }
          $filesize = filesize($v_header['filename']);
          if ($filesize != $v_header['size']) {
            $this
              ->_error('Extracted file ' . $v_header['filename'] . ' does not have the correct file size \'' . $filesize . '\' (' . $v_header['size'] . ' expected). Archive may be corrupted.');
            return false;
          }
        }
      }
      else {
        $this
          ->_jumpBlock(ceil($v_header['size'] / 512));
      }
    }
    else {
      $this
        ->_jumpBlock(ceil($v_header['size'] / 512));
    }

    /* TBC : Seems to be unused ...
       if ($this->_compress)
         $v_end_of_file = @gzeof($this->_file);
       else
         $v_end_of_file = @feof($this->_file);
         */
    if ($v_listing || $v_extract_file || $v_extraction_stopped) {

      // ----- Log extracted files
      if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) {
        $v_file_dir = '';
      }
      if (substr($v_header['filename'], 0, 1) == '/' && $v_file_dir == '') {
        $v_file_dir = '/';
      }
      $p_list_detail[$v_nb++] = $v_header;
      if (is_array($p_file_list) && count($p_list_detail) == count($p_file_list)) {
        return true;
      }
    }
  }
  return true;
}