/** * Installs the templates of this package. */ public function install() { parent::install(); // extract files.tar to temp folder $tag = $this->installation->getXMLTag('templates'); $sourceFile = $this->installation->getArchive()->extractTar($tag['cdata'], 'templates_'); // create file handler $fileHandler = new TemplatesFileHandler($this->installation); // extract content of files.tar $packageDir = FileUtil::addTrailingSlash(FileUtil::getRealPath(WCF_DIR . $this->installation->getPackage()->getDir())); try { $fileInstaller = $this->installation->extractFiles($packageDir . 'templates/', $sourceFile, $fileHandler); } catch (SystemException $e) { if (!@file_exists(WCF_DIR . 'acp/templates/packageInstallationFileInstallationFailed.tpl')) { // workaround for wcf 1.0 to 1.1 update throw $e; } else { WCF::getTPL()->assign(array('exception' => $e)); WCF::getTPL()->display('packageInstallationFileInstallationFailed'); exit; } } // look if tpl files are to be overwritten by this update, // and if so, check if these files have been patched before ... require_once WCF_DIR . 'lib/acp/package/plugin/TemplatePatchPackageInstallationPlugin.class.php'; $tar = new Tar($sourceFile); $files = $tar->getContentList(); $templatePatch = new TemplatePatchPackageInstallationPlugin($this->installation); $templatePatch->repatch($files); // delete temporary sourceArchive @unlink($sourceFile); }
/** * @see Installer::createFile() */ protected function createFile($file, $index, Tar $tar) { $tar->extract($index, $this->targetDir . $file); if (IS_APACHE_MODULE || !is_writeable($this->targetDir . $file)) { $this->makeWriteable($this->targetDir . $file); } }
/** * Extracts package.xml from archive */ private function readArchive() { // wcf imports require_once WCF_DIR . 'lib/system/io/Tar.class.php'; // extract the package.xml from archive $archive = new Tar($this->file); $this->file = $this->source->buildDirectory . '/package.' . sha1(microtime()) . '.xml'; $archive->extract('package.xml', $this->file); $archive->close(); }
/** * Starts the extracting of the files. */ protected function install() { $this->checkTargetDir(); $this->createTargetDir(); // open source archive $tar = new Tar($this->source); // distinct directories and files $directories = array(); $files = array(); foreach ($tar->getContentList() as $index => $file) { if (empty($this->folder) || StringUtil::indexOf($file['filename'], $this->folder) === 0) { if (!empty($this->folder)) { $file['filename'] = StringUtil::replace($this->folder, '', $file['filename']); } // remove leading slash $file['filename'] = FileUtil::removeLeadingSlash($file['filename']); if ($file['type'] == 'folder') { // remove trailing slash $directories[] = FileUtil::removeTrailingSlash($file['filename']); } else { $files[$index] = $file['filename']; } } } $this->checkFiles($files); // now create the directories $errors = array(); foreach ($directories as $dir) { try { $this->createDir($dir); } catch (SystemException $e) { $errors[] = array('file' => $dir, 'code' => $e->getCode(), 'message' => $e->getMessage()); } } // now untar all files foreach ($files as $index => $file) { try { $this->createFile($file, $index, $tar); } catch (SystemException $e) { $errors[] = array('file' => $file, 'code' => $e->getCode(), 'message' => $e->getMessage()); } } if (count($errors) > 0) { throw new SystemException('error(s) during the installation of the files.', 11111, $errors); } $this->logFiles($files); // close tar $tar->close(); }
/** * @see Installer::createFile() */ protected function createFile($file, $index, Tar $tar) { if (!file_exists($this->targetDir . $file)) { if (!$this->ftp->put($this->ftpPath . $file, $this->dummyUploadFile, FTP_ASCII)) { throw new SystemException("cannot create file '" . $this->ftpPath . $file . "' at ftp server.", 14007); } } if (IS_APACHE_MODULE || !is_writeable($this->targetDir . $file)) { $this->makeWriteable($this->ftpPath . $file); } $tar->extract($index, $this->targetDir . $file); }
/** * Installs the templates of this package. */ public function install() { parent::install(); // extract files.tar to temp folder $tag = $this->installation->getXMLTag('templates'); $sourceFile = $this->installation->getArchive()->extractTar($tag['cdata'], 'templates_'); // create file handler $fileHandler = new TemplatesFileHandler($this->installation); // extract content of files.tar $packageDir = FileUtil::addTrailingSlash(FileUtil::getRealPath(WCF_DIR . $this->installation->getPackage()->getDir())); $fileInstaller = $this->installation->extractFiles($packageDir . 'templates/', $sourceFile, $fileHandler); // look if tpl files are to be overwritten by this update, // and if so, check if these files have been patched before ... require_once WCF_DIR . 'lib/acp/package/plugin/TemplatePatchPackageInstallationPlugin.class.php'; $tar = new Tar($sourceFile); $files = $tar->getContentList(); $templatePatch = new TemplatePatchPackageInstallationPlugin($this->installation); $templatePatch->repatch($files); // delete temporary sourceArchive @unlink($sourceFile); }
/** * @param string $file * @param bool $location * @return bool */ public static function unpack($file, $loc = false) { $t = new Tar(); if (!$t->openTar(self::normalizePath($file))) { return false; } if (!$loc) { return $t->files; } if (!file_exists(self::normalizePath($loc))) { mkdir($loc, 0777, true); } if (substr($loc, 0, -1) != '/') { $loc .= '/'; } foreach ($t->files as $file) { if (!file_exists(self::normalizePath($loc . dirname($file['name'])))) { mkdir(self::normalizePath($loc . dirname($file['name'])), 0777, true); } file_put_contents(self::normalizePath($loc . $file['name']), $file['file']); chmod(self::normalizePath($loc . $file['name']), 0777); } return true; }
if (isset($_GET['showIcon'])) { readFileResource('showIcon', TMP_DIR . 'install/files/icon/'); } // show css from temp folder if (isset($_GET['showCSS'])) { readFileResource('showCSS', TMP_DIR . 'install/files/acp/style/setup/'); } // show fonts from temp folder if (isset($_GET['showFont'])) { readFileResource('showFont', TMP_DIR . 'install/files/font/'); } // check whether setup files are already unzipped if (!file_exists(TMP_DIR . 'install/files/lib/system/WCFSetup.class.php')) { // try to unzip all setup files into temp folder $tar = new Tar(SETUP_FILE); $contentList = $tar->getContentList(); if (empty($contentList)) { throw new SystemException("Can not unpack 'WCFSetup.tar.gz'. File is probably broken."); } foreach ($contentList as $file) { foreach ($neededFilesPattern as $pattern) { if (preg_match($pattern, $file['filename'])) { // create directory if not exists $dir = TMP_DIR . dirname($file['filename']); if (!@is_dir($dir)) { @mkdir($dir, 0777, true); @chmod($dir, 0777); }
/** * Gets the package name of the first standalone application in WCFSetup.tar.gz. */ protected static function getPackageName() { // get package name $tar = new Tar(SETUP_FILE); foreach ($tar->getContentList() as $file) { if ($file['type'] != 'folder' && StringUtil::indexOf($file['filename'], 'install/packages/') === 0) { $packageFile = basename($file['filename']); $packageName = preg_replace('!\\.(tar\\.gz|tgz|tar)$!', '', $packageFile); if ($packageName != 'com.woltlab.wcf') { try { $archive = new PackageArchive(TMP_DIR . TMP_FILE_PREFIX . $packageFile); $archive->openArchive(); self::$setupPackageName = $archive->getPackageInfo('packageName'); $archive->getTar()->close(); break; } catch (SystemException $e) { } } } } $tar->close(); // assign package name WCF::getTPL()->assign('setupPackageName', self::$setupPackageName); }
/** * Imports a style. * * @param string $filename * @param integer $packageID * @param StyleEditor $style * @return StyleEditor */ public static function import($filename, $packageID = PACKAGE_ID, $style = null) { // open file require_once WCF_DIR . 'lib/system/io/Tar.class.php'; $tar = new Tar($filename); // get style data $data = self::readStyleData($tar); // fix safe_mode problem $iconsLocation = FileUtil::addTrailingSlash($data['variables']['global.icons.location']); $imagesLocation = $data['variables']['global.images.location']; if (FileUtil::getSafeMode() && !empty($data['images']) && !file_exists(WCF_DIR . $imagesLocation)) { $oldImagesLocation = $imagesLocation; $imagesLocation = 'images/' . str_replace('/', '-', preg_replace('!^images/!', '', $imagesLocation)); foreach ($data['variables'] as $name => $value) { $data['variables'][$name] = str_replace($oldImagesLocation, $imagesLocation, $value); } $data['variables']['global.images.location'] = 'images/'; if (strpos($data['variables']['page.logo.image'], '../') !== false) { $data['variables']['page.logo.image'] = 'images/' . basename($data['variables']['page.logo.image']); } } // create template pack $templatePackID = 0; if (!empty($data['templates'])) { // create template pack $originalTemplatePackName = $templatePackName = $data['name']; $templatePackFolderName = preg_replace('/[^a-z0-9_-]/i', '', $templatePackName); if (empty($templatePackFolderName)) { $templatePackFolderName = 'generic' . StringUtil::substring(StringUtil::getRandomID(), 0, 8); } $originalTemplatePackFolderName = $templatePackFolderName; // get unique template pack name $i = 1; do { $sql = "SELECT\tCOUNT(*) AS count\n\t\t\t\t\tFROM\twcf" . WCF_N . "_template_pack\n\t\t\t\t\tWHERE\ttemplatePackName = '" . escapeString($templatePackName) . "'"; $row = WCF::getDB()->getFirstRow($sql); if (!$row['count']) { break; } $templatePackName = $originalTemplatePackName . '_' . $i; $i++; } while (true); // get unique folder name $i = 1; do { $sql = "SELECT\tCOUNT(*) AS count\n\t\t\t\t\tFROM\twcf" . WCF_N . "_template_pack\n\t\t\t\t\tWHERE\ttemplatePackFolderName = '" . escapeString(FileUtil::addTrailingSlash($templatePackFolderName)) . "'\n\t\t\t\t\t\tAND parentTemplatePackID = 0"; $row = WCF::getDB()->getFirstRow($sql); if (!$row['count']) { break; } $templatePackFolderName = $originalTemplatePackFolderName . '_' . $i; $i++; } while (true); $sql = "INSERT INTO\twcf" . WCF_N . "_template_pack\n\t\t\t\t\t\t(templatePackName, templatePackFolderName)\n\t\t\t\tVALUES\t\t('" . escapeString($templatePackName) . "', '" . FileUtil::addTrailingSlash(escapeString($templatePackFolderName)) . "')"; WCF::getDB()->sendQuery($sql); $templatePackID = WCF::getDB()->getInsertID("wcf" . WCF_N . "_template_pack", 'templatePackID'); } // save style if ($style !== null) { $style->update($data['name'], $data['variables'], $templatePackID, $data['description'], $data['version'], $data['date'], ($data['image'] ? 'images/' : '') . $data['image'], $data['copyright'], $data['license'], $data['authorName'], $data['authorURL']); } else { $style = self::create($data['name'], $data['variables'], $templatePackID, $data['description'], $data['version'], $data['date'], ($data['image'] ? 'images/' : '') . $data['image'], $data['copyright'], $data['license'], $data['authorName'], $data['authorURL'], 0, $packageID); } // import preview image if (!empty($data['image'])) { $i = $tar->getIndexByFilename($data['image']); if ($i !== false) { $tar->extract($i, WCF_DIR . 'images/' . $data['image']); @chmod(WCF_DIR . 'images/' . $data['image'], 0777); } } // import images if (!empty($data['images'])) { // create images folder if necessary if (!file_exists(WCF_DIR . $imagesLocation) && !FileUtil::getSafeMode()) { @mkdir(WCF_DIR . $data['variables']['global.images.location'], 0777); @chmod(WCF_DIR . $data['variables']['global.images.location'], 0777); } $i = $tar->getIndexByFilename($data['images']); if ($i !== false) { // extract images tar $destination = FileUtil::getTemporaryFilename('images_'); $tar->extract($i, $destination); // open images tar $imagesTar = new Tar($destination); $contentList = $imagesTar->getContentList(); foreach ($contentList as $key => $val) { if ($val['type'] == 'file') { $imagesTar->extract($key, WCF_DIR . $imagesLocation . basename($val['filename'])); @chmod(WCF_DIR . $imagesLocation . basename($val['filename']), 0666); } } // delete tmp file $imagesTar->close(); @unlink($destination); } } // import icons if (!empty($data['icons']) && $iconsLocation != 'icon/') { $i = $tar->getIndexByFilename($data['icons']); if ($i !== false) { // extract icons tar $destination = FileUtil::getTemporaryFilename('icons_'); $tar->extract($i, $destination); // open icons tar and group icons by package $iconsTar = new Tar($destination); $contentList = $iconsTar->getContentList(); $packageToIcons = array(); foreach ($contentList as $key => $val) { if ($val['type'] == 'file') { $folders = explode('/', $val['filename']); $packageName = array_shift($folders); if (!isset($packageToIcons[$packageName])) { $packageToIcons[$packageName] = array(); } $packageToIcons[$packageName][] = array('index' => $val['index'], 'filename' => implode('/', $folders)); } } // copy icons foreach ($packageToIcons as $package => $icons) { // try to find package $sql = "SELECT\t*\n\t\t\t\t\t\tFROM\twcf" . WCF_N . "_package\n\t\t\t\t\t\tWHERE\tpackage = '" . escapeString($package) . "'\n\t\t\t\t\t\t\tAND standalone = 1"; $result = WCF::getDB()->sendQuery($sql); while ($row = WCF::getDB()->fetchArray($result)) { // get icon path $iconDir = FileUtil::getRealPath(WCF_DIR . $row['packageDir']) . $iconsLocation; // create icon path if (!file_exists($iconDir)) { @mkdir($iconDir, 0777); @chmod($iconDir, 0777); } // copy icons foreach ($icons as $icon) { $iconsTar->extract($icon['index'], $iconDir . $icon['filename']); } } } // delete tmp file $iconsTar->close(); @unlink($destination); } } // import templates if (!empty($data['templates'])) { $i = $tar->getIndexByFilename($data['templates']); if ($i !== false) { // extract templates tar $destination = FileUtil::getTemporaryFilename('templates_'); $tar->extract($i, $destination); // open templates tar and group templates by package $templatesTar = new Tar($destination); $contentList = $templatesTar->getContentList(); $packageToTemplates = array(); foreach ($contentList as $key => $val) { if ($val['type'] == 'file') { $folders = explode('/', $val['filename']); $packageName = array_shift($folders); if (!isset($packageToTemplates[$packageName])) { $packageToTemplates[$packageName] = array(); } $packageToTemplates[$packageName][] = array('index' => $val['index'], 'filename' => implode('/', $folders)); } } // copy templates foreach ($packageToTemplates as $package => $templates) { // try to find package $sql = "SELECT\t*\n\t\t\t\t\t\tFROM\twcf" . WCF_N . "_package\n\t\t\t\t\t\tWHERE\tpackage = '" . escapeString($package) . "'\n\t\t\t\t\t\t\tAND standalone = 1"; $result = WCF::getDB()->sendQuery($sql); while ($row = WCF::getDB()->fetchArray($result)) { // get icon path $templatesDir = FileUtil::addTrailingSlash(FileUtil::getRealPath(WCF_DIR . $row['packageDir']) . 'templates/' . $templatePackFolderName); // create template path if (!file_exists($templatesDir)) { @mkdir($templatesDir, 0777); @chmod($templatesDir, 0777); } // copy templates foreach ($templates as $template) { $templatesTar->extract($template['index'], $templatesDir . $template['filename']); $sql = "INSERT INTO\twcf" . WCF_N . "_template\n\t\t\t\t\t\t\t\t\t\t(packageID, templateName, templatePackID)\n\t\t\t\t\t\t\t\tVALUES\t\t(" . $row['packageID'] . ", '" . escapeString(str_replace('.tpl', '', $template['filename'])) . "', " . $templatePackID . ")"; WCF::getDB()->sendQuery($sql); } } } // delete tmp file $templatesTar->close(); @unlink($destination); } } $tar->close(); return $style; }
foreach ($fileListing as $file) { print "Archived file: {$file}\n"; } // Append the file /etc/passwd // [ tar -rf myArchive.tar /etc/passwd ] $ar->append("/etc/passwd"); // Delete the directory and it's contents // [ tar -f myArchive.tar --delete MyDirectory ] $ar->delete("MyDirectory"); // Append another directory .. $ar->append("AnotherDirectory"); // .. and show detailed file permissions plus the file or directory path. $fileListing = $ar->getList(); foreach ($fileListing as $file) { $entry = $ar->getArchivedEntry($file); print $entry->getPermissionsString(); if ($entry->isDirectory()) { print " [ " . $entry->getPath() . " ]\n"; } else { print " " . $entry->getPath() . "\n"; } } // Extract the whole archive to the tmp directory $ar->extractTo("/tmp/"); // Extract the passwd file to the file: /tmp/passwd-backup $ar->extractTo("/tmp/passwd-backup", "passwd"); // Extracting the gzipped archive: compressedTar.tgz to the tmp directory: $ar = new Tar("compress.zlib://compressedTar.tgz"); $ar->extractTo("/tmp/"); // .. and append the password file. $ar->append("/etc/passwd");
/** * Validates upload file. */ protected function validateFile() { $savedSmilies = 0; WCF::getTPL()->assignByRef('savedSmilies', $savedSmilies); // upload smiley(s) if ($this->upload && $this->upload['error'] != 4) { if ($this->upload['error'] != 0) { throw new UserInputException('upload', 'uploadFailed'); } // try to open file as an archive if (preg_match('/(?:tar\\.gz|tgz|tar)$/i', $this->upload['name'])) { $errors = array(); $tar = new Tar($this->upload['tmp_name']); foreach ($tar->getContentList() as $file) { if ($file['type'] != 'folder') { // extract to tmp dir $tmpname = FileUtil::getTemporaryFilename('smiley_'); $tar->extract($file['index'], $tmpname); try { // find filename $i = 0; $destination = WCF_DIR . 'images/smilies/' . basename($file['filename']); while (file_exists($destination)) { $destination = preg_replace('/((?=\\.[^\\.]*$)|(?=$))/', '_' . ++$i, $destination, 1); } // save $this->smileyIDs[] = SmileyEditor::create($tmpname, $destination, 'upload', null, null, $this->showOrder ? $this->showOrder : null, $this->smileyCategoryID)->smileyID; $savedSmilies++; } catch (UserInputException $e) { $errors[] = array('filename' => $file['filename'], 'errorType' => $e->getType()); } } } $tar->close(); @unlink($this->upload['tmp_name']); if (count($errors)) { throw new UserInputException('upload', $errors); } else { if ($savedSmilies == 0) { throw new UserInputException('upload', 'emptyArchive'); } } } else { $this->validateTitle(); $this->validateCode(); // import as image file $this->smileyIDs[] = SmileyEditor::create($this->upload['tmp_name'], WCF_DIR . 'images/smilies/' . basename($this->upload['name']), 'upload', $this->title, $this->code, $this->showOrder ? $this->showOrder : null, $this->smileyCategoryID)->smileyID; $savedSmilies++; } } else { if (!empty($this->filename)) { if (!file_exists($this->filename)) { throw new UserInputException('filename', 'notFound'); } // copy smileys from a dir if (is_dir($this->filename)) { $errors = array(); $this->filename = FileUtil::addTrailingSlash($this->filename); $handle = opendir($this->filename); while (($file = readdir($handle)) !== false) { if ($file != '.' && $file != '..' && is_file($this->filename . $file)) { try { // find filename $i = 0; $destination = WCF_DIR . 'images/smilies/' . $file; while (file_exists($destination)) { $destination = preg_replace('/((?=\\.[^\\.]*$)|(?=$))/', '_' . ++$i, $destination, 1); } // save $this->smileyIDs[] = SmileyEditor::create($this->filename . $file, $destination, 'filename', null, null, $this->showOrder ? $this->showOrder : null, $this->smileyCategoryID)->smileyID; $savedSmilies++; } catch (UserInputException $e) { $errors[] = array('filename' => $this->filename . $file, 'errorType' => $e->getType()); } } } if (count($errors)) { throw new UserInputException('filename', $errors); } else { if ($savedSmilies == 0) { throw new UserInputException('filename', 'emptyFolder'); } } } else { $this->validateTitle(); $this->validateCode(); $this->smileyIDs[] = SmileyEditor::create($this->filename, WCF_DIR . 'images/smilies/' . basename($this->filename), 'filename', $this->title, $this->code, $this->showOrder ? $this->showOrder : null, $this->smileyCategoryID)->smileyID; $savedSmilies++; } } else { throw new UserInputException('upload'); } } }
nextstep: if (is_int(stripos($control_c_raw_data[0][0], 'control.tar.gz'))) { if (!is_dir("../tmp/")) { mkdir("../tmp/"); } if (!is_dir("../tmp/" . $r_id)) { mkdir("../tmp/" . $r_id); } $t_path = "../tmp/" . $r_id . '/control.tar.gz'; if (file_exists($t_path)) { unlink($t_path); } $control_tar_handle = fopen($t_path, 'w'); fputs($control_tar_handle, $control_c_raw_data[0][6]); fclose($control_tar_handle); $control_tar = new Tar(); $control_tar->load($t_path); $control_array = $control_tar->contents(); $control_data = $control_array['control']['data']; $plain_array = explode("\n", $control_data); foreach ($plain_array as $line) { if (strlen(trim(substr($line, 0, 1))) == 0) { $t_value = trim($line); $t_package[$t_key] .= "\n" . $t_value; } else { if (preg_match("#^Package|Source|Version|Priority|Section|Essential|Maintainer|Pre-Depends|Depends|Recommends|Suggests|Conflicts|Provides|Replaces|Enhances|Architecture|Filename|Size|Installed-Size|Description|Origin|Bugs|Name|Author|Homepage|Website|Depiction|Icon|Tag|Sponsor#", $line)) { preg_match("#^([^:]*?):(.*)#", $line, $t_matches); $t_key = trim($t_matches[1]); $t_value = trim($t_matches[2]); $t_package[$t_key] = $t_value; }
$new_filepath = "../tmp/" . $r_id . "/Applications/Cydia.app/Sections/" . $new_filename; copy("../icons/" . $icon_assoc['Icon'], $new_filepath); chmod($new_filepath, 0755); foreach ($new_filenames as $filename) { $new_tar->add_file("/Applications/Cydia.app/Sections/" . $filename, 0755, file_get_contents($new_filepath)); } } } $new_tar->save($new_path); $result = $raw_data->replace("data.tar.gz", $new_path); if (!$result) { $alert = __('Icon package template rewriting failed!'); goto endlabel; } else { $control_path = "../tmp/" . $r_id . "/control.tar.gz"; $control_tar = new Tar(); $f_Package = "Package: " . (defined("AUTOFILL_PRE") ? AUTOFILL_PRE : '') . "sourceicon\nArchitecture: iphoneos-arm\nName: Source Icon\nVersion: 0.1-1\nAuthor: " . (defined("AUTOFILL_SEO") ? AUTOFILL_SEO . (defined("AUTOFILL_EMAIL") ? ' <' . AUTOFILL_EMAIL . '>' : '') : 'DCRM <*****@*****.**>') . "\nSponsor: " . (defined("AUTOFILL_MASTER") ? AUTOFILL_MASTER . (defined("AUTOFILL_EMAIL") ? ' <' . AUTOFILL_EMAIL . '>' : '') : 'i_82 <http://82flex.com>') . "\nMaintainer: " . (defined("AUTOFILL_MASTER") ? AUTOFILL_MASTER . (defined("AUTOFILL_EMAIL") ? ' <' . AUTOFILL_EMAIL . '>' : '') : 'i_82 <http://82flex.com>') . "\nDescription: Custom Empty Source Icon Package"; $control_tar->add_file("control", "", $f_Package); $control_tar->save($control_path); $result = $raw_data->replace("control.tar.gz", $control_path); if (!$result) { $alert = __('Icon package template rewriting failed!'); goto endlabel; } else { $result = rename($deb_path, "../upload/" . AUTOFILL_PRE . "sourceicon_" . time() . ".deb"); if (!$result) { $alert = __('Icon package template repositioning failed!'); goto endlabel; } header("Location: manage.php"); exit;
public function test_cleanPath() { $tar = new Tar(); $tests = array('/foo/bar' => 'foo/bar', '/foo/bar/' => 'foo/bar', 'foo//bar' => 'foo/bar', 'foo/0/bar' => 'foo/0/bar', 'foo/../bar' => 'bar', 'foo/bang/bang/../../bar' => 'foo/bar', 'foo/../../bar' => 'bar', 'foo/.././../bar' => 'bar'); foreach ($tests as $in => $out) { $this->assertEquals($out, $tar->cleanPath($in), "Input: {$in}"); } }
} /* nonzero indicates error */ public function save_one($f, $name, $file) { $file['size'] = strlen($file['data']); $file['name'] = $name; $hdr_data = $this->header_pack($file); if ($hdr_data === FALSE) { return -1; } unset($file['size']); /* size is implicit in data */ unset($file['name']); /* name is array key */ $f->record_save($hdr_data); $f->record_save($file['data']); return 0; } } if (isset($argv)) { $tar = new Tar(); if (count($argv) > 1) { $tar->load($argv[1]); } else { $tar->load("/dev/stdin"); } foreach ($tar->files as $name => $file) { echo $name . "\n"; } $tar->save("somefile.tar.gz", ".gz"); }
/** * Unzips compressed package archives. * * @param string $archive filename * @return string new filename */ public static function unzipPackageArchive($archive) { if (!FileUtil::isURL($archive)) { $tar = new Tar($archive); $tar->close(); if ($tar->isZipped()) { $tmpName = FileUtil::getTemporaryFilename('package_'); if (FileUtil::uncompressFile($archive, $tmpName)) { return $tmpName; } } } return $archive; }
$filename_array = $raw_data->listfiles(); foreach ($filename_array as $filename) { if (is_int(stripos($filename, 'control.tar.gz'))) { $control_c_raw_data = $raw_data->getfile($filename); $innername = $filename; goto nextstep; } } nextstep: if (is_int(stripos($control_c_raw_data[0][0], 'control.tar.gz'))) { $control_tar_path = "../tmp/" . $r_id . "/old.tar.gz"; $control_tar_handle = fopen($control_tar_path, 'w'); fputs($control_tar_handle, $control_c_raw_data[0][6]); fclose($control_tar_handle); $control_tar = new Tar(); $new_tar = new Tar(); $control_tar->load($control_tar_path); $control_array = $control_tar->contents(); foreach ($control_array as $c_key => $c_value) { $alert .= "ยท " . sprintf(__('Processing file: %s'), htmlspecialchars($c_key)) . "<br />"; if ($c_key != "control") { $new_tar->add_file($c_key, "", $control_array[$c_key]['data']); } } $new_path = "../tmp/" . $r_id . "/control.tar.gz"; $new_tar->add_file("control", "", $f_Package); $new_tar->save($new_path); } $replace_result = $raw_data->replace($innername, $new_path); $success = true; if ($replace_result) {
/** * @see Form::validate() */ public function validate() { parent::validate(); // validate group id $group = new Group($this->groupID); if (!$group->groupID) { throw new UserInputException('groupID'); } // category if ($this->avatarCategoryID != 0) { $avatarCategory = new AvatarCategory($this->avatarCategoryID); if (!$avatarCategory->avatarCategoryID) { throw new UserInputException('avatarCategoryID'); } } $savedAvatars = 0; WCF::getTPL()->assignByRef('savedAvatars', $savedAvatars); // upload avatar(s) if ($this->upload && $this->upload['error'] != 4) { if ($this->upload['error'] != 0) { throw new UserInputException('upload', 'uploadFailed'); } // try to open file as an archive if (preg_match('/(?:tar\\.gz|tgz|tar)$/i', $this->upload['name'])) { $errors = array(); $tar = new Tar($this->upload['tmp_name']); foreach ($tar->getContentList() as $file) { if ($file['type'] != 'folder') { // extract to tmp dir $tmpname = FileUtil::getTemporaryFilename('avatar_'); $tar->extract($file['index'], $tmpname); try { $this->avatarIDs[] = AvatarEditor::create($tmpname, $file['filename'], 'upload', 0, $this->groupID, $this->neededPoints, $this->avatarCategoryID); $savedAvatars++; } catch (UserInputException $e) { $errors[] = array('filename' => $file['filename'], 'errorType' => $e->getType()); } } } $tar->close(); @unlink($this->upload['tmp_name']); if (count($errors)) { throw new UserInputException('upload', $errors); } else { if ($savedAvatars == 0) { throw new UserInputException('upload', 'emptyArchive'); } } } else { // import as image file $this->avatarIDs[] = AvatarEditor::create($this->upload['tmp_name'], $this->upload['name'], 'upload', 0, $this->groupID, $this->neededPoints, $this->avatarCategoryID); $savedAvatars++; } } else { if (!empty($this->filename)) { if (!file_exists($this->filename)) { throw new UserInputException('filename', 'notFound'); } // copy avatars from a dir if (is_dir($this->filename)) { $errors = array(); $this->filename = FileUtil::addTrailingSlash($this->filename); $handle = opendir($this->filename); while (($file = readdir($handle)) !== false) { if ($file != '.' && $file != '..' && is_file($this->filename . $file)) { try { $this->avatarIDs[] = AvatarEditor::create($this->filename . $file, $this->filename . $file, 'filename', 0, $this->groupID, $this->neededPoints, $this->avatarCategoryID); $savedAvatars++; } catch (UserInputException $e) { $errors[] = array('filename' => $this->filename . $file, 'errorType' => $e->getType()); } } } if (count($errors)) { throw new UserInputException('filename', $errors); } else { if ($savedAvatars == 0) { throw new UserInputException('filename', 'emptyFolder'); } } } else { $this->avatarIDs[] = AvatarEditor::create($this->filename, $this->filename, 'filename', 0, $this->groupID, $this->neededPoints, $this->avatarCategoryID); $savedAvatars++; } } else { throw new UserInputException('upload'); } } }
/** * Tests Tar::handlesCompression */ public function testHandlesCompression() { $path = realpath(__DIR__ . '/../../../_files/bin'); $tar = new Tar($path); $this->assertFalse($tar->handlesCompression()); $tar->useCompression('zip'); $this->assertFalse($tar->handlesCompression()); $tar->useCompression('gzip'); $this->assertTrue($tar->handlesCompression()); }