/** * @group Core */ public function testRelativePath() { clearstatcache(); $extractDir = PIWIK_USER_PATH . '/tmp/latest/'; $test = 'relative'; $filename = dirname(__FILE__) . '/Unzip/' . $test . '.zip'; if (class_exists('ZipArchive', false)) { $unzip = Unzip::factory('ZipArchive', $filename); $res = $unzip->extract($extractDir); $this->assertEquals(1, count($res)); $this->assertFileExists($extractDir . $test . '.txt'); $this->assertFileNotExists(dirname(__FILE__) . '/' . $test . '.txt'); $this->assertFileNotExists(dirname(__FILE__) . '/../../tests/' . $test . '.txt'); unlink($extractDir . $test . '.txt'); $unzip = new ZipArchive($filename); $res = $unzip->extract($extractDir); $this->assertEquals(1, count($res)); $this->assertFileExists($extractDir . $test . '.txt'); $this->assertFileNotExists(dirname(__FILE__) . '/' . $test . '.txt'); $this->assertFileNotExists(dirname(__FILE__) . '/../../tests/' . $test . '.txt'); unlink($extractDir . $test . '.txt'); } $unzip = Unzip::factory('PclZip', $filename); $res = $unzip->extract($extractDir); $this->assertEquals(1, count($res)); $this->assertFileExists($extractDir . $test . '.txt'); $this->assertFileNotExists(dirname(__FILE__) . '/' . $test . '.txt'); $this->assertFileNotExists(dirname(__FILE__) . '/../../tests/' . $test . '.txt'); unlink($extractDir . $test . '.txt'); $unzip = new PclZip($filename); $res = $unzip->extract($extractDir); $this->assertEquals(1, count($res)); $this->assertFileExists($extractDir . $test . '.txt'); $this->assertFileNotExists(dirname(__FILE__) . '/' . $test . '.txt'); $this->assertFileNotExists(dirname(__FILE__) . '/../../tests/' . $test . '.txt'); unlink($extractDir . $test . '.txt'); }
private function oneClick_Unpack() { $pathExtracted = PIWIK_USER_PATH . self::PATH_TO_EXTRACT_LATEST_VERSION; $pathExtracted = SettingsPiwik::rewriteTmpPathWithInstanceId($pathExtracted); $this->pathRootExtractedPiwik = $pathExtracted . 'piwik'; if (file_exists($this->pathRootExtractedPiwik)) { Filesystem::unlinkRecursive($this->pathRootExtractedPiwik, true); } $archive = Unzip::factory('PclZip', $this->pathPiwikZip); if (0 == ($archive_files = $archive->extract($pathExtracted))) { throw new Exception(Piwik::translate('CoreUpdater_ExceptionArchiveIncompatible', $archive->errorInfo())); } if (0 == count($archive_files)) { throw new Exception(Piwik::translate('CoreUpdater_ExceptionArchiveEmpty')); } unlink($this->pathPiwikZip); }
/** * @param $pluginZipFile * @param $pathExtracted * @throws \Exception */ private function extractPluginFiles($pluginZipFile, $pathExtracted) { $archive = Unzip::factory('PclZip', $pluginZipFile); $this->removeFolderIfExists($pathExtracted); if (0 == ($pluginFiles = $archive->extract($pathExtracted))) { throw new PluginInstallerException(Piwik::translate('CoreUpdater_ExceptionArchiveIncompatible', $archive->errorInfo())); } if (0 == count($pluginFiles)) { throw new PluginInstallerException(Piwik::translate('Plugin Zip File Is Empty')); } }
/** * Unzips a downloaded GeoIP database. Only unzips .gz & .tar.gz files. * * @param string $path Path to zipped file. * @param bool $unlink Whether to unlink archive or not. * @throws Exception */ public static function unzipDownloadedFile($path, $unlink = false) { $parts = explode('.', basename($path)); $filenameStart = $parts[0]; $dbFilename = $filenameStart . '.dat'; $tempFilename = $filenameStart . '.dat.new'; $outputPath = GeoIp::getPathForGeoIpDatabase($tempFilename); // extract file if (substr($path, -7, 7) == '.tar.gz') { // find the .dat file in the tar archive $unzip = Unzip::factory('tar.gz', $path); $content = $unzip->listContent(); if (empty($content)) { throw new Exception(Piwik::translate('UserCountry_CannotListContent', array("'{$path}'", $unzip->errorInfo()))); } $datFile = null; foreach ($content as $info) { $archivedPath = $info['filename']; if (basename($archivedPath) === $dbFilename) { $datFile = $archivedPath; } } if ($datFile === null) { throw new Exception(Piwik::translate('UserCountry_CannotFindGeoIPDatabaseInArchive', array($dbFilename, "'{$path}'"))); } // extract JUST the .dat file $unzipped = $unzip->extractInString($datFile); if (empty($unzipped)) { throw new Exception(Piwik::translate('UserCountry_CannotUnzipDatFile', array("'{$path}'", $unzip->errorInfo()))); } // write unzipped to file $fd = fopen($outputPath, 'wb'); fwrite($fd, $unzipped); fclose($fd); } else { if (substr($path, -3, 3) == '.gz') { $unzip = Unzip::factory('gz', $path); $success = $unzip->extract($outputPath); if ($success !== true) { throw new Exception(Piwik::translate('UserCountry_CannotUnzipDatFile', array("'{$path}'", $unzip->errorInfo()))); } } else { $ext = end(explode(basename($path), '.', 2)); throw new Exception(Piwik::translate('UserCountry_UnsupportedArchiveType', "'{$ext}'")); } } try { // test that the new archive is a valid GeoIP database $dbType = GeoIp::getGeoIPDatabaseTypeFromFilename($dbFilename); if ($dbType === false) { throw new Exception("Unexpected GeoIP archive file name '{$path}'."); } $customDbNames = array('loc' => array(), 'isp' => array(), 'org' => array()); $customDbNames[$dbType] = array($tempFilename); $phpProvider = new Php($customDbNames); $location = self::getTestLocationCatchPhpErrors($phpProvider); if (empty($location) || self::$unzipPhpError !== null) { if (self::$unzipPhpError !== null) { list($errno, $errstr, $errfile, $errline) = self::$unzipPhpError; Log::info("GeoIPAutoUpdater: Encountered PHP error when testing newly downloaded" . " GeoIP database: %s: %s on line %s of %s.", $errno, $errstr, $errline, $errfile); } throw new Exception(Piwik::translate('UserCountry_ThisUrlIsNotAValidGeoIPDB')); } // delete the existing GeoIP database (if any) and rename the downloaded file $oldDbFile = GeoIp::getPathForGeoIpDatabase($dbFilename); if (file_exists($oldDbFile)) { unlink($oldDbFile); } $tempFile = GeoIp::getPathForGeoIpDatabase($tempFilename); rename($existing = $tempFile, $newName = $oldDbFile); // delete original archive if ($unlink) { unlink($path); } } catch (Exception $ex) { // remove downloaded files if (file_exists($outputPath)) { unlink($outputPath); } unlink($path); throw $ex; } }
private function decompressArchive($archiveFile) { $extractionPath = $this->tmpPath . self::PATH_TO_EXTRACT_LATEST_VERSION; $extractedArchiveDirectory = $extractionPath . 'piwik'; // Remove previous decompressed archive if (file_exists($extractedArchiveDirectory)) { Filesystem::unlinkRecursive($extractedArchiveDirectory, true); } $archive = Unzip::factory('PclZip', $archiveFile); $archiveFiles = $archive->extract($extractionPath); if (0 == $archiveFiles) { throw new Exception($this->translator->translate('CoreUpdater_ExceptionArchiveIncompatible', $archive->errorInfo())); } if (0 == count($archiveFiles)) { throw new Exception($this->translator->translate('CoreUpdater_ExceptionArchiveEmpty')); } unlink($archiveFile); return $extractedArchiveDirectory; }
protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln("Starting to fetch latest language pack"); $dialog = $this->getHelperSet()->get('dialog'); $cookieFile = self::getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt'; @unlink($cookieFile); $username = $input->getOption('username'); $password = $input->getOption('password'); while (!file_exists($cookieFile)) { if (empty($username)) { $username = $dialog->ask($output, 'What is your oTrance username? '); } if (empty($password)) { $password = $dialog->askHiddenResponse($output, 'What is your oTrance password? '); } // send login request to oTrance and save the login cookie $curl = curl_init('http://translations.piwik.org/public/index/login'); curl_setopt($curl, CURLOPT_POSTFIELDS, sprintf("user=%s&pass=%s&autologin=1", $username, $password)); curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_exec($curl); curl_close($curl); if (strpos(file_get_contents($cookieFile), 'oTranCe_autologin') !== false) { break; } $username = null; $password = null; @unlink($cookieFile); $output->writeln("Invalid oTrance credentials. Please try again..."); } // send request to create a new download package using the cookie file $createNewPackage = true; if ($input->isInteractive()) { $createNewPackage = $dialog->askConfirmation($output, 'Shall we create a new language pack? '); } if ($createNewPackage) { $curl = curl_init('http://translations.piwik.org/public/export/update.all'); curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_exec($curl); curl_close($curl); } // request download page to search for available packages $curl = curl_init('http://translations.piwik.org/public/downloads/'); curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); curl_close($curl); preg_match_all('/language\\_pack\\-[0-9]{8}\\-[0-9]{6}\\.tar\\.gz/i', $response, $matches); if (empty($matches[0])) { $output->writeln("No packages found for download. Please try again."); return; } $downloadPackage = array_shift($matches[0]); $continueWithPackage = true; if ($input->isInteractive()) { $continueWithPackage = $dialog->askConfirmation($output, "Found language pack {$downloadPackage}. Proceed? "); } if (!$continueWithPackage) { $output->writeln('Aborted.'); return; } // download language pack $packageHandle = fopen(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz', 'w'); $curl = curl_init('http://translations.piwik.org/public/downloads/download/file/' . $downloadPackage); curl_setopt($curl, CURLOPT_COOKIEFILE, self::getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt'); curl_setopt($curl, CURLOPT_FILE, $packageHandle); curl_exec($curl); curl_close($curl); @unlink($cookieFile); $output->writeln("Extracting package..."); $unzipper = Unzip::factory('tar.gz', self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz'); $unzipper->extract(self::getDownloadPath()); if (!$input->getOption('keep-english')) { @unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'en.php'); @unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'en.json'); } @unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz'); $filesToConvert = _glob(self::getDownloadPath() . DIRECTORY_SEPARATOR . '*.php'); $output->writeln("Converting downloaded php files to json"); $progress = $this->getHelperSet()->get('progress'); $progress->start($output, count($filesToConvert)); foreach ($filesToConvert as $filename) { require_once $filename; $basename = explode(".", basename($filename)); $nested = array(); foreach ($translations as $key => $value) { list($plugin, $nkey) = explode("_", $key, 2); $nested[$plugin][$nkey] = $value; } $translations = $nested; $data = json_encode($translations, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); $newFile = sprintf("%s/%s.json", self::getDownloadPath(), $basename[0]); file_put_contents($newFile, $data); @unlink($filename); $progress->advance(); } $progress->finish(); $output->writeln("Finished fetching new language files from oTrance"); }