public function install() { global $core; $zip = new fileUnzip($this->zip_file); if ($zip->isEmpty()) { $zip->close(); unlink($this->zip_file); throw new Exception(__('Empty plugin zip file.')); } $zip_root_dir = $zip->getRootDir(); if (!$zip_root_dir) { // try to find a root anyway if all dirs start with same pattern $dirs = $zip->getDirsList(); $n = 0; $zip_root_dir = substr($dirs[0], 0, strpos($dirs[0], '/')); foreach ($dirs as $dir) { if ($zip_root_dir != substr($dirs[0], 0, strpos($dirs[0], '/'))) { $n++; } } if ($n > 0) { $zip_root_dir = false; } } if ($zip_root_dir != false) { $target = dirname($this->zip_file); $destination = $target . '/' . $zip_root_dir; $plugin_js = $zip_root_dir . '/plugin.js'; $has_plugin_js = $zip->hasFile($plugin_js); } else { $target = dirname($this->zip_file) . '/' . preg_replace('/\\.([^.]+)$/', '', basename($this->zip_file)); $destination = $target; $plugin_js = 'plugin.js'; $has_plugin_js = $zip->hasFile($plugin_js); } if ($core->blog->settings->dcCKEditorAddons->check_validity) { if (!$has_plugin_js) { $zip->close(); unlink($this->zip_file); throw new Exception(__('The zip file does not appear to be a valid CKEditor addon.')); } } if (!is_dir($destination)) { files::makeDir($destination, true); } $zip->unzipAll($target); $zip->close(); unlink($this->zip_file); }
/** * Backups changed files before an update. * * @param string $sZipFile * @param string $sZipDigests * @param string $sRoot * @param string $sRootDigests * @param string $sDest * @throws Exception * @return boolean */ public function backup($sZipFile, $sZipDigests, $sRoot, $sRootDigests, $sDest) { if (!is_readable($sZipFile)) { throw new Exception(__('c_a_update_archive_not_found')); } if (!is_readable($sRootDigests)) { @unlink($sZipFile); throw new Exception(__('c_a_update_unable_read_digests')); } # Stop everything if a backup already exists and can not be overrided if (!is_writable(dirname($sDest)) && !file_exists($sDest)) { throw new Exception(__('c_a_update_root_directory_not_writable')); } if (file_exists($sDest) && !is_writable($sDest)) { return false; } $b_fp = @fopen($sDest, 'wb'); if ($b_fp === false) { return false; } $oZip = new fileUnzip($sZipFile); $b_zip = new fileZip($b_fp); if (!$oZip->hasFile($sZipDigests)) { @unlink($sZipFile); throw new Exception(__('c_a_update_downloaded_file_not_valid_archive')); } $opts = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES; $cur_digests = file($sRootDigests, $opts); $new_digests = explode("\n", $oZip->unzip($sZipDigests)); $aNewFiles = $this->getNewFiles($cur_digests, $new_digests); $oZip->close(); unset($opts, $cur_digests, $new_digests, $oZip); $aNotReadable = array(); if (!empty($this->aForcedFiles)) { $aNewFiles = array_merge($aNewFiles, $this->aForcedFiles); } foreach ($aNewFiles as $file) { if (!$file || !file_exists($sRoot . '/' . $file)) { continue; } try { $b_zip->addFile($sRoot . '/' . $file, $file); } catch (Exception $e) { $aNotReadable[] = $file; } } # If only one file is not readable, stop everything now if (!empty($aNotReadable)) { $e = new Exception('Some files are not readable.', self::ERR_FILES_UNREADABLE); $e->bad_files = $aNotReadable; throw $e; } $b_zip->write(); fclose($b_fp); $b_zip->close(); return true; }
public static function installPackage($zip_file, dcModules &$modules) { $zip = new fileUnzip($zip_file); $zip->getList(false, '#(^|/)(__MACOSX|\\.svn|\\.hg|\\.git|\\.DS_Store|\\.directory|Thumbs\\.db)(/|$)#'); $zip_root_dir = $zip->getRootDir(); $define = ''; if ($zip_root_dir != false) { $target = dirname($zip_file); $destination = $target . '/' . $zip_root_dir; $define = $zip_root_dir . '/_define.php'; $has_define = $zip->hasFile($define); } else { $target = dirname($zip_file) . '/' . preg_replace('/\\.([^.]+)$/', '', basename($zip_file)); $destination = $target; $define = '_define.php'; $has_define = $zip->hasFile($define); } if ($zip->isEmpty()) { $zip->close(); unlink($zip_file); throw new Exception(__('Empty module zip file.')); } if (!$has_define) { $zip->close(); unlink($zip_file); throw new Exception(__('The zip file does not appear to be a valid Dotclear module.')); } $ret_code = 1; if (!is_dir($destination)) { try { files::makeDir($destination, true); $sandbox = clone $modules; $zip->unzip($define, $target . '/_define.php'); $sandbox->resetModulesList(); $sandbox->requireDefine($target, basename($destination)); unlink($target . '/_define.php'); $new_errors = $sandbox->getErrors(); if (!empty($new_errors)) { $new_errors = is_array($new_errors) ? implode(" \n", $new_errors) : $new_errors; throw new Exception($new_errors); } files::deltree($destination); } catch (Exception $e) { $zip->close(); unlink($zip_file); files::deltree($destination); throw new Exception($e->getMessage()); } } else { # test for update $sandbox = clone $modules; $zip->unzip($define, $target . '/_define.php'); $sandbox->resetModulesList(); $sandbox->requireDefine($target, basename($destination)); unlink($target . '/_define.php'); $new_modules = $sandbox->getModules(); if (!empty($new_modules)) { $tmp = array_keys($new_modules); $id = $tmp[0]; $cur_module = $modules->getModules($id); if (!empty($cur_module) && (defined('DC_DEV') && DC_DEV === true || dcUtils::versionsCompare($new_modules[$id]['version'], $cur_module['version'], '>', true))) { # delete old module if (!files::deltree($destination)) { throw new Exception(__('An error occurred during module deletion.')); } $ret_code = 2; } else { $zip->close(); unlink($zip_file); throw new Exception(sprintf(__('Unable to upgrade "%s". (older or same version)'), basename($destination))); } } else { $zip->close(); unlink($zip_file); throw new Exception(sprintf(__('Unable to read new _define.php file'))); } } $zip->unzipAll($target); $zip->close(); unlink($zip_file); return $ret_code; }
/** * Install a theme from a zip file * * @param string $zip_file * @param oktThemes $oThemes */ public static function installPackage($zip_file, oktThemes $oThemes) { $zip = new fileUnzip($zip_file); $zip->getList(false, '#(^|/)(__MACOSX|\\.svn|\\.DS_Store|Thumbs\\.db)(/|$)#'); $zip_root_dir = $zip->getRootDir(); if ($zip_root_dir !== false) { $target = dirname($zip_file); $destination = $target . '/' . $zip_root_dir; $define = $zip_root_dir . '/_define.php'; $has_define = $zip->hasFile($define); } else { $target = dirname($zip_file) . '/' . preg_replace('/\\.([^.]+)$/', '', basename($zip_file)); $destination = $target; $define = '_define.php'; $has_define = $zip->hasFile($define); } if ($zip->isEmpty()) { $zip->close(); unlink($zip_file); throw new Exception(__('Empty theme zip file.')); } if (!$has_define) { $zip->close(); unlink($zip_file); throw new Exception(__('The zip file does not appear to be a valid theme.')); } $ret_code = 1; if (is_dir($destination)) { throw new Exception(__('The theme allready exists, you can not update a theme.')); /* copy($target.'/_define.php', $target.'/_define.php.bak'); # test for update $sandbox = clone $oThemes; $zip->unzip($define, $target.'/_define.php'); $sandbox->resetThemesList(); $sandbox->requireDefine($target,basename($destination)); unlink($target.'/_define.php'); $new_themes = $sandbox->getThemesList(); $old_themes = $oThemes->getThemesList(); if (!empty($new_themes)) { $tmp = array_keys($new_themes); $id = $tmp[0]; $cur_theme = $old_themes[$id]; if (!empty($cur_theme) && $new_themes[$id]['version'] != $cur_theme['version']) { # delete old theme if (!files::deltree($destination)) { throw new Exception(__('An error occurred during theme deletion.')); } $ret_code = 2; } else { $zip->close(); unlink($zip_file); if (file_exists($target.'/_define.php.bak')) { rename($target.'/_define.php.bak', $target.'/_define.php'); } throw new Exception(sprintf(__('Unable to upgrade "%s". (same version)'),basename($destination))); } } else { $zip->close(); unlink($zip_file); if (file_exists($target.'/_define.php.bak')) { rename($target.'/_define.php.bak', $target.'/_define.php'); } throw new Exception(sprintf(__('Unable to read new _define.php file'))); } */ } $zip->unzipAll($target); $zip->close(); unlink($zip_file); return $ret_code; }
continue; } $iItemId = $okt->galleries->items->addItem($okt->galleries->items->openItemCursor(array('gallery_id' => $iGalleryId, 'active' => 1)), $aItemLocalesData); $sDestination = $okt->galleries->upload_dir . '/img/items/' . $iItemId . '/1.' . $sFileExtension; files::makeDir(dirname($sDestination), true); $oZip->unzip($sFileName, $sDestination); $aNewImagesInfos = $okt->galleries->items->getImageUploadInstance()->buildImagesInfos($iItemId, array(1 => basename($sDestination))); if (isset($aNewImagesInfos[1])) { $aNewItemImages = $aNewImagesInfos[1]; $aNewItemImages['original_name'] = utf8_encode(basename($sFileName)); } else { $aNewItemImages = array(); } $okt->galleries->items->updImages($iItemId, $aNewItemImages); } $oZip->close(); files::deltree($sTempDir); } catch (Exception $e) { $okt->error->set($e->getMessage()); } if ($okt->error->isEmpty()) { $okt->galleries->items->regenMinImages($iGalleryId); $okt->redirect('module.php?m=galleries&action=items&gallery_id=' . $iGalleryId); } } /* Affichage ----------------------------------------------------------*/ $okt->page->addButton('galleriesBtSt', array('permission' => true, 'title' => __('c_c_action_Go_back'), 'url' => $iGalleryId ? 'module.php?m=galleries&action=items&gallery_id=' . $iGalleryId : 'module.php?m=galleries&action=index', 'ui-icon' => 'arrowreturnthick-1-w'), 'before'); $okt->page->addGlobalTitle(__('m_galleries_zip_main_title')); # Récupération de la liste complète des galeries $rsGalleries = $okt->galleries->tree->getGalleries(array('language' => $okt->user->language, 'active' => 2));
/** Backups changed files before an update. */ public function backup($zip_file, $zip_digests, $root, $root_digests, $dest) { if (!is_readable($zip_file)) { throw new Exception(__('Archive not found.')); } if (!is_readable($root_digests)) { @unlink($zip_file); throw new Exception(__('Unable to read current digests file.')); } # Stop everything if a backup already exists and can not be overrided if (!is_writable(dirname($dest)) && !file_exists($dest)) { throw new Exception(__('Root directory is not writable.')); } if (file_exists($dest) && !is_writable($dest)) { return false; } $b_fp = @fopen($dest, 'wb'); if ($b_fp === false) { return false; } $zip = new fileUnzip($zip_file); $b_zip = new fileZip($b_fp); if (!$zip->hasFile($zip_digests)) { @unlink($zip_file); throw new Exception(__('Downloaded file seems not to be a valid archive.')); } $opts = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES; $cur_digests = file($root_digests, $opts); $new_digests = explode("\n", $zip->unzip($zip_digests)); $new_files = $this->getNewFiles($cur_digests, $new_digests); $zip->close(); unset($opts, $cur_digests, $new_digests, $zip); $not_readable = array(); if (!empty($this->forced_files)) { $new_files = array_merge($new_files, $this->forced_files); } foreach ($new_files as $file) { if (!$file || !file_exists($root . '/' . $file)) { continue; } try { $b_zip->addFile($root . '/' . $file, $file); } catch (Exception $e) { $not_readable[] = $file; } } # If only one file is not readable, stop everything now if (!empty($not_readable)) { $e = new Exception('Some files are not readable.', self::ERR_FILES_UNREADABLE); $e->bad_files = $not_readable; throw $e; } $b_zip->write(); fclose($b_fp); $b_zip->close(); return true; }
/** Returns zip file content @param f <b>fileRecord</b> fileRecord object @return <b>array</b> */ public function getZipContent($f) { $zip = new fileUnzip($f->file); $list = $zip->getList(false, '#(^|/)(__MACOSX|\\.svn|\\.DS_Store|Thumbs\\.db)(/|$)#'); $zip->close(); return $list; }
private function unzip($file) { $zip = new fileUnzip($file); if ($zip->isEmpty()) { $zip->close(); return false; //throw new Exception(__('File is empty or not a compressed file.')); } foreach ($zip->getFilesList() as $zip_file) { # Check zipped file name if (substr($zip_file, -4) != '.txt') { continue; } # Check zipped file contents $content = $zip->unzip($zip_file); if (strpos($content, '///DOTCLEAR|') !== 0) { unset($content); continue; } $target = path::fullFromRoot($zip_file, dirname($file)); # Check existing files with same name if (file_exists($target)) { $zip->close(); unset($content); throw new Exception(__('Another file with same name exists.')); } # Extract backup content if (file_put_contents($target, $content) === false) { $zip->close(); unset($content); throw new Exception(__('Failed to extract backup file.')); } $zip->close(); unset($content); # Return extracted file name return $target; } $zip->close(); throw new Exception(__('No backup in compressed file.')); }
private static function installLocalWidget($categoryId, $userId, $uploadedFilePath) { try { $unziper = new fileUnzip($uploadedFilePath); $archiveContent = $unziper->getFilesList(); $tempFolder = './widgets/' . mktime(); $alreadyInstalled = false; if (!self::isFileInArchive($archiveContent, 'config.xml')) { throw new FileException(MwwException::MODEL, 'Unable to find config.xml in the provided archive'); } else { if (!self::isFileInArchive($archiveContent, 'index.html') && !self::isFileInArchive($archiveContent, 'index.xul')) { throw new FileException(MwwException::MODEL, 'Unable to find index.html in the provided archive'); } else { // The mandatory files are in the archive. // We unzip the document in the widget repository under an arbitrary folder. $unziper->unzipAll($tempFolder); $unziper->close(); // parse the manifest. $manifestPath = $tempFolder . '/config.xml'; $doc = new DOMDocument(); $doc->preserveWhiteSpace = false; if (!@$doc->load($manifestPath)) { throw new XMLParsingException(MwwException::MODEL, 'Unable to parse XML file. Document not well formed.'); } // validate the manifest. if (!@$doc->schemaValidate('./schema/manifest.xsd')) { throw new XMLValidationException(MwwException::MODEL, "The XML File isn't valid according to the XML Schema."); } // test if the id is unique (test if the directory exists). $id = $doc->getElementsByTagName('widget')->item(0)->getAttribute('id'); // getting name/title of the widget. if ($doc->getElementsByTagName('name')->length) { $name = $doc->getElementsByTagName('name')->item(0)->nodeValue; } else { if ($doc->getElementsByTagName('title')->length) { $name = $doc->getElementsByTagName('title')->item(0)->nodeValue; } else { throw new XMLValidationException(MwwException::MODEL, "The XML File is not valid. The mandatory title or name element is missing."); } } // getting widget description. $description = null; if ($doc->getElementsByTagName('description')->length) { $description = $doc->getElementsByTagName('description')->item(0)->nodeValue; } // is widget authentication enabled or disabled ? $widgetAuth = false; $authKey = null; if ($doc->getElementsByTagName('widget_authentication')->length) { $authValue = $doc->getElementsByTagName('widget_authentication')->item(0)->nodeValue; if ($authValue == 'enabled') { $widgetAuth = true; // create the key. $crypto = new Rijndael(); $authKey = $crypto->generateKey(); } } $targetFolder = './widgets/' . $id; if (file_exists($targetFolder)) { $alreadyInstalled = true; throw new FileException(MwwException::MODEL, 'A widget with the id \'' . $id . '\' already exists.'); } // create the widget directory (actually we simply rename the temporary directory). if (!@rename($tempFolder, $targetFolder)) { throw new FileException(MwwException::MODEL, 'The widget directory could not be created'); } $nameToReturn = $name; // -- Persistant data access section. $db = DbUtil::accessFactory(); $categoryId = $categoryId != 0 ? $db->escape($categoryId) : 'NULL'; $userId = $db->escape($userId); $name = $db->escape($name); $description = $db->escape($description); $generatedKey = $widgetAuth ? "'{$authKey}'" : 'NULL'; // insert the widget into the database. if (!$db->execute("INSERT INTO `widgets` (`widgetid`, `widgetname`, `visible`, `copname`, `category`, `description`, `authkey`) VALUES ('{$id}', '{$name}', 0, (SELECT `copname` FROM `users` WHERE `id`= {$userId}), {$categoryId}, '{$description}', {$generatedKey})")) { throw new DBException(MwwException::MODEL, "Unable to insert new widget '{$id}' information in persistant data"); } // -- End of Persistant data access section. // End of the installation. Everything was fine. return $nameToReturn; } } } catch (Exception $ex) { if (isset($tempFolder) && is_dir($tempFolder)) { Util::full_rmdir($tempFolder); } if (isset($targetFolder) && is_dir($targetFolder) && !$alreadyInstalled) { Util::full_rmdir($targetFolder); } throw $ex; } }