/** * Check if input (an array of $_FILES) are .tar or .zip files, if they * are then these get unpacked and returns an managed as $_FILES (returning * an array with the same structure $_FILES uses and move pics to /tmp) * * @access public * @param array $files $_FILES * @return array $_FILES format */ function UnpackFiles($files) { if (!is_array($files)) { return array(); } $cleanFiles = array(); $tmpDir = sys_get_temp_dir(); $counter = 1; require_once PEAR_PATH . 'File/Archive.php'; foreach ($files as $key => $file) { if (empty($file['tmp_name'])) { continue; } $ext = strrchr($file['name'], '.'); switch ($ext) { case '.gz': $ext = '.tgz'; break; case '.bz2': case '.bzip2': $ext = '.tbz'; break; } $ext = strtolower(ltrim($ext, '.')); if (File_Archive::isKnownExtension($ext)) { $tmpArchiveName = $tmpDir . DIRECTORY_SEPARATOR . $file['name']; if (!move_uploaded_file($file['tmp_name'], $tmpArchiveName)) { continue; } $reader = File_Archive::read($tmpArchiveName); $source = File_Archive::readArchive($ext, $reader); if (!PEAR::isError($source)) { while ($source->next()) { $destFile = $tmpDir . DIRECTORY_SEPARATOR . basename($source->getFilename()); $sourceFile = $tmpArchiveName . '/' . $source->getFilename(); $extract = File_Archive::extract($sourceFile, $tmpDir); if (PEAR::IsError($extract)) { continue; } $cleanFiles['photo' . $counter] = array('name' => basename($source->getFilename()), 'type' => $source->getMime(), 'tmp_name' => $destFile, 'size' => @filesize($destFile), 'error' => 0); $counter++; } } } else { $cleanFiles['photo' . $counter] = $file; $counter++; } } return $cleanFiles; }
/** * Attempt to change the current source (if the current file is an archive) * If this is the case, push the current source onto the stack and make the * good archive reader the current source. A file is considered as an * archive if its extension is one of tar, gz, zip, tgz * * @return bool whether the source has been pushed or not * @access private */ function push() { if ($this->uncompressionLevel >= 0 && $this->baseDirCompressionLevel !== null && count($this->readers) >= $this->uncompressionLevel) { return false; } // Check the extension of the file (maybe we need to uncompress it?) $filename = $this->source->getFilename(); $extensions = explode('.', strtolower($filename)); $reader =& $this->source; $nbUncompressions = 0; while (($extension = array_pop($extensions)) !== null) { $nbUncompressions++; unset($next); $next = File_Archive::readArchive($extension, $reader, $nbUncompressions == 1); if ($next === false) { $extensions = array(); } else { unset($reader); $reader =& $next; } } if ($nbUncompressions == 1) { return false; } else { $this->readers[count($this->readers)] =& $this->source; unset($this->source); $this->source = new File_Archive_Reader_AddBaseName($filename, $reader); return true; } }
/** * Extract archive Files * * @access public * @param array $files $_FILES array * @param string $dest Destination directory(include end directory separator) * @param bool $extractToDir Create separate directory for extracted files * @param bool $overwrite Overwrite directory if exist * @param int $max_size Max size of file * @return bool Returns TRUE on success or FALSE on failure */ static function ExtractFiles($files, $dest, $extractToDir = true, $overwrite = true, $max_size = null) { if (empty($files) || !is_array($files)) { return new Jaws_Error(_t('GLOBAL_ERROR_UPLOAD'), __FUNCTION__); } if (isset($files['name'])) { $files = array($files); } require_once PEAR_PATH . 'File/Archive.php'; foreach ($files as $key => $file) { if (isset($file['error']) && !empty($file['error']) || !isset($file['name'])) { return new Jaws_Error(_t('GLOBAL_ERROR_UPLOAD_' . $file['error']), __FUNCTION__); } if (empty($file['tmp_name'])) { continue; } $ext = strrchr($file['name'], '.'); $filename = substr($file['name'], 0, -strlen($ext)); if (false !== stristr($filename, '.tar')) { $filename = substr($filename, 0, strrpos($filename, '.')); switch ($ext) { case '.gz': $ext = '.tgz'; break; case '.bz2': case '.bzip2': $ext = '.tbz'; break; default: $ext = '.tar' . $ext; } } $ext = strtolower(substr($ext, 1)); if (!File_Archive::isKnownExtension($ext)) { return new Jaws_Error(_t('GLOBAL_ERROR_UPLOAD_INVALID_FORMAT', $file['name']), __FUNCTION__); } if ($extractToDir) { $dest = $dest . $filename; } if ($extractToDir && !Jaws_Utils::mkdir($dest)) { return new Jaws_Error(_t('GLOBAL_ERROR_FAILED_CREATING_DIR', $dest), __FUNCTION__); } if (!Jaws_Utils::is_writable($dest)) { return new Jaws_Error(_t('GLOBAL_ERROR_FAILED_DIRECTORY_UNWRITABLE', $dest), __FUNCTION__); } $archive = File_Archive::readArchive($ext, $file['tmp_name']); if (PEAR::isError($archive)) { return new Jaws_Error($archive->getMessage(), __FUNCTION__); } $writer = File_Archive::_convertToWriter($dest); $result = $archive->extract($writer); if (PEAR::isError($result)) { return new Jaws_Error($result->getMessage(), __FUNCTION__); } //@unlink($file['tmp_name']); } return true; }
function init() { $oKTConfig =& KTConfig::getSingleton(); $sBasedir = $oKTConfig->get("urls/tmpDirectory"); $sTmpPath = tempnam($sBasedir, 'archiveimportstorage'); if ($sTmpPath === false) { return PEAR::raiseError(_kt("Could not create temporary directory for archive storage")); } if (!file_exists($this->sZipPath)) { return PEAR::raiseError(_kt("Archive file given does not exist")); } unlink($sTmpPath); mkdir($sTmpPath, 0700); $this->sBasePath = $sTmpPath; // Set environment language to output character encoding $sOutputEncoding = $oKTConfig->get('export/encoding', 'UTF-8'); $loc = $sOutputEncoding; putenv("LANG={$loc}"); putenv("LANGUAGE={$loc}"); $loc = setlocale(LC_ALL, $loc); // File Archive doesn't unzip properly so sticking to the original unzip functionality if ($this->sExtension == 'zip') { // ** Original zip functionality $sUnzipCommand = KTUtil::findCommand("import/unzip", "unzip"); if (empty($sUnzipCommand)) { return PEAR::raiseError(_kt("unzip command not found on system")); } $aArgs = array($sUnzipCommand, "-q", "-n", "-d", $sTmpPath, $this->sZipPath); $aRes = KTUtil::pexec($aArgs); if ($aRes['ret'] !== 0) { return PEAR::raiseError(_kt("Could not retrieve contents from zip storage")); } } else { File_Archive::extract(File_Archive::readArchive($this->sExtension, File_Archive::readUploadedFile('file')), $dst = $sTmpPath); } }
/** * Create a writer that can be used to append files to an archive inside a source * If the archive can't be found in the source, it will be created * If source is set to null, File_Archive::toFiles will be assumed * If type is set to null, the type of the archive will be determined looking at * the extension in the URL * stat is the array of stat (returned by stat() PHP function of Reader getStat()) * to use if the archive must be created * * This function allows to create or append data to nested archives. Only one * archive will be created and if your creation requires creating several nested * archives, a PEAR error will be returned * * After this call, $source will be closed and should not be used until the * returned writer is closed. * * @param File_Archive_Reader $source A reader where some files will be appended * @param string $URL URL to reach the archive in the source. * if $URL is null, a writer to append files to the $source reader will * be returned * @param bool $unique If true, the duplicate files will be deleted on close * Default is false (and setting it to true may have some performance * consequences) * @param string $type Extension of the archive (or null to use the one in the URL) * @param array $stat Used only if archive is created, array of stat as returned * by PHP stat function or Reader getStat function: stats of the archive) * Time (index 9) will be overwritten to current time * @return File_Archive_Writer a writer that you can use to append files to the reader */ function appenderFromSource(&$toConvert, $URL = null, $unique = null, $type = null, $stat = array()) { $source =& File_Archive::_convertToReader($toConvert); if (PEAR::isError($source)) { return $source; } if ($unique == null) { $unique = File_Archive::getOption("appendRemoveDuplicates"); } //Do not report the fact that the archive does not exist as an error PEAR::pushErrorHandling(PEAR_ERROR_RETURN); if ($URL === null) { $result =& $source; } else { if ($type === null) { $result = File_Archive::_readSource($source, $URL . '/', $reachable, $baseDir); } else { $result = File_Archive::readArchive($type, File_Archive::_readSource($source, $URL, $reachable, $baseDir)); } } PEAR::popErrorHandling(); if (!PEAR::isError($result)) { if ($unique) { require_once dirname(__FILE__) . "/Archive/Writer/UniqueAppender.php"; return new File_Archive_Writer_UniqueAppender($result); } else { return $result->makeAppendWriter(); } } //The source can't be found and has to be created $stat[9] = $stat['mtime'] = time(); if (empty($baseDir)) { if ($source !== null) { $writer =& $source->makeWriter(); } else { $writer =& File_Archive::toFiles(); } if (PEAR::isError($writer)) { return $writer; } PEAR::pushErrorHandling(PEAR_ERROR_RETURN); $result = File_Archive::toArchive($reachable, $writer, $type); PEAR::popErrorHandling(); if (PEAR::isError($result)) { $result = File_Archive::toFiles($reachable); } } else { $reachedSource = File_Archive::readSource($source, $reachable); if (PEAR::isError($reachedSource)) { return $reachedSource; } $writer = $reachedSource->makeWriter(); if (PEAR::isError($writer)) { return $writer; } PEAR::pushErrorHandling(PEAR_ERROR_RETURN); $result = File_Archive::toArchive($baseDir, $writer, $type); PEAR::popErrorHandling(); if (PEAR::isError($result)) { require_once dirname(__FILE__) . "/Archive/Writer/AddBaseName.php"; $result = new File_Archive_Writer_AddBaseName($baseDir, $writer); if (PEAR::isError($result)) { return $result; } } } return $result; }
function _testReadArchive() { $source = File_Archive::readArchive('tar', File_Archive::read('up.tar')); while ($source->next()) { echo $source->getFilename() . "\n"; } }
/** * Attempt to change the current source (if the current file is an archive) * If this is the case, push the current source onto the stack and make the * good archive reader the current source. A file is considered as an * archive if its extension is one of tar, gz, zip, tgz * * @return bool whether the source has been pushed or not * @access private */ function push() { $filename = $this->source->getFilename(); if (substr($filename, -1) == '/') { //it's a directory return false; } if ($this->uncompressionLevel >= 0 && $this->baseDirCompressionLevel !== null && count($this->readers) >= $this->uncompressionLevel) { return false; } // Check the extension of the file (maybe we need to uncompress it?) $extensions = explode('.', strtolower($filename)); $reader =& $this->source; $nbUncompressions = 0; while (($extension = array_pop($extensions)) !== null) { $nbUncompressions++; unset($next); $next = File_Archive::readArchive($extension, $reader, $nbUncompressions == 1); if ($next === false) { $extensions = array(); } else { unset($reader); $reader =& $next; } } if ($nbUncompressions == 1) { return false; } else { $this->readers[count($this->readers)] =& $this->source; unset($this->source); $this->source = new File_Archive_Reader_ChangeName_AddDirectory($filename, $reader); // 解凍するファイル内に圧縮ファイルが存在すると、圧縮ファイルが消えてしまうため修正 by S.Nakajima $this->baseDirCompressionLevel = $nbUncompressions; return true; } }