elFinderVolumeFTP::_extract PHP Method

_extract() protected method

Extract files from archive
Author: Dmitry (dio) Levashov,
Author: Alexey Sukhotin
protected _extract ( string $path, array $arc ) : true
$path string archive path
$arc array archiver command and arguments (same as in $this->archivers)
return true
    protected function _extract($path, $arc)
    {
        $dir = $this->tempDir();
        if (!$dir) {
            return false;
        }
        $basename = $this->_basename($path);
        $localPath = $dir . DIRECTORY_SEPARATOR . $basename;
        if (!ftp_get($this->connect, $localPath, $path, FTP_BINARY)) {
            //cleanup
            $this->rmdirRecursive($dir);
            return false;
        }
        $this->unpackArchive($localPath, $arc);
        $filesToProcess = elFinderVolumeFTP::listFilesInDirectory($dir, true);
        // no files - extract error ?
        if (empty($filesToProcess)) {
            return false;
        }
        $this->archiveSize = 0;
        // find symlinks
        $symlinks = $this->_findSymlinks($dir);
        if ($symlinks) {
            $this->rmdirRecursive($dir);
            return $this->setError(array_merge($this->error, array(elFinder::ERROR_ARC_SYMLINKS)));
        }
        // check max files size
        if ($this->options['maxArcFilesSize'] > 0 && $this->options['maxArcFilesSize'] < $this->archiveSize) {
            $this->rmdirRecursive($dir);
            return $this->setError(elFinder::ERROR_ARC_MAXSIZE);
        }
        $extractTo = $this->extractToNewdir;
        // 'auto', ture or false
        // archive contains one item - extract in archive dir
        $name = '';
        $src = $dir . DIRECTORY_SEPARATOR . $filesToProcess[0];
        if (($extractTo === 'auto' || !$extractTo) && count($filesToProcess) === 1 && is_file($src)) {
            $name = $filesToProcess[0];
        } else {
            if ($extractTo === 'auto' || $extractTo) {
                // for several files - create new directory
                // create unique name for directory
                $src = $dir;
                $name = basename($path);
                if (preg_match('/\\.((tar\\.(gz|bz|bz2|z|lzo))|cpio\\.gz|ps\\.gz|xcf\\.(gz|bz2)|[a-z0-9]{1,4})$/i', $name, $m)) {
                    $name = substr($name, 0, strlen($name) - strlen($m[0]));
                }
                $test = $this->_joinPath(dirname($path), $name);
                if ($this->stat($test)) {
                    $name = $this->uniqueName(dirname($path), $name, '-', false);
                }
            }
        }
        if ($name !== '' && is_file($src)) {
            $result = $this->_joinPath(dirname($path), $name);
            if (!ftp_put($this->connect, $result, $src, FTP_BINARY)) {
                $this->rmdirRecursive($dir);
                return false;
            }
        } else {
            $dstDir = $this->_dirname($path);
            $result = array();
            if (is_dir($src)) {
                if (!($dstDir = $this->_mkdir($dstDir, $name))) {
                    $this->rmdirRecursive($dir);
                    return false;
                }
                $result[] = $dstDir;
            }
            foreach ($filesToProcess as $name) {
                $name = rtrim($name, DIRECTORY_SEPARATOR);
                $src = $dir . DIRECTORY_SEPARATOR . $name;
                if (is_dir($src)) {
                    $p = dirname($name);
                    $name = basename($name);
                    if (!($target = $this->_mkdir($this->_joinPath($dstDir, $p), $name))) {
                        $this->rmdirRecursive($dir);
                        return false;
                    }
                } else {
                    $target = $this->_joinPath($dstDir, $name);
                    if (!ftp_put($this->connect, $target, $src, FTP_BINARY)) {
                        $this->rmdirRecursive($dir);
                        return false;
                    }
                }
                $result[] = $target;
            }
            if (!$result) {
                $this->rmdirRecursive($dir);
                return false;
            }
        }
        is_dir($dir) && $this->rmdirRecursive($dir);
        $this->clearcache();
        return $result ? $result : false;
    }