/** * 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); }
/** * 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(); }
/** * 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); }
/** * @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'); } } }
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); } $tar->extract($file['index'], TMP_DIR . $file['filename']);
/** * 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); }
/** * 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'); } } }