/** * 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(); }
/** * @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'); } } }
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); } $tar->extract($file['index'], TMP_DIR . $file['filename']); } } } $tar->close(); // create cache folders @mkdir(TMP_DIR . 'setup/lang/cache/', 0777); @chmod(TMP_DIR . 'setup/lang/cache/', 0777); @mkdir(TMP_DIR . 'setup/template/compiled/', 0777); @chmod(TMP_DIR . 'setup/template/compiled/', 0777); } if (!class_exists('wcf\system\WCFSetup')) { throw new SystemException("Can not find class 'WCFSetup'"); } // start setup new \wcf\system\WCFSetup();
/** * 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; }
/** * 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); }
/** * 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; }
/** * 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'); } } }
public function test_createlongpathgnu() { $tar = new Tar(); $tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); $path = ''; for ($i = 0; $i < 20; $i++) { $path .= '1234567890/'; } $path = rtrim($path, '/'); $tar->create($tmp, Tar::COMPRESS_NONE); $tar->addData("{$path}/test.txt", 'testcontent1'); $tar->close(); $this->assertTrue(filesize($tmp) > 30); //arbitrary non-zero number $data = file_get_contents($tmp); // We should find the complete path/filename and a longlink entry $this->assertTrue(strpos($data, 'testcontent1') !== false, 'content in TAR'); $this->assertTrue(strpos($data, 'test.txt') !== false, 'filename in TAR'); $this->assertTrue(strpos($data, $path) !== false, 'path in TAR'); $this->assertTrue(strpos($data, "{$path}/test.txt") !== false, 'full filename in TAR'); $this->assertTrue(strpos($data, '@LongLink') !== false, '@LongLink in TAR'); @unlink($tmp); }