/** * Build a zip with all files. * * @param string $strZipPath Path for the file * @throws Exception */ public function buildUpdateZip($strZipPath) { // Open archive $this->objZipArchive = new ZipArchiveCto(); if (($mixError = $this->objZipArchive->open($strZipPath, ZipArchiveCto::CREATE)) !== true) { throw new Exception($GLOBALS['TL_LANG']['MSC']['error'] . ": " . $this->objZipArchive->getErrorDescription($mixError)); } // Add files $this->addFiles(); // Add db update file $this->addDatabase(); $this->objZipArchive->close(); }
/** * Unzip files * * @param string $strRestoreFile Path to the zip file * @return mixes True - If ervething is okay, Array - If some files could not be extract to a given path. * @throws Exception if the zip file was not able to open. */ public function runRestore($strRestoreFile) { $objZipArchive = new ZipArchiveCto(); if (($mixError = $objZipArchive->open($strRestoreFile)) !== true) { $strError = sprintf("%s %s || Status (%s): %s || System (%s): %s ", $GLOBALS['TL_LANG']['ERR']['cant_extract_file'], $objZipArchive->getErrorDescription($mixError), $objZipArchive->status, $objZipArchive->getErrorDescription($objZipArchive->status), $objZipArchive->statusSys, $objZipArchive->getErrorDescription($objZipArchive->statusSys)); throw new \RuntimeException($strError); } if ($objZipArchive->numFiles == 0) { return; } $arrErrorFiles = array(); for ($i = 0; $i < $objZipArchive->numFiles; $i++) { $filename = $objZipArchive->getNameIndex($i); if (!$objZipArchive->extractTo("/", $filename)) { $arrErrorFiles[] = $filename; } } $objZipArchive->close(); if (count($arrErrorFiles) == 0) { return true; } else { return $arrErrorFiles; } }
/** * Update the database. */ protected function updateDatabase($strZipPath) { $arrError = array(); // New archive $objZipArchive = new ZipArchiveCto(); // Open archive if (($mixError = $objZipArchive->open($strZipPath, ZipArchiveCto::CREATE)) !== true) { throw new Exception($GLOBALS['TL_LANG']['MSC']['error'] . ": " . $objZipArchive->getErrorDescription($mixError)); } // Read XML $xmlReader = new XMLReader(); $xmlReader->XML($objZipArchive->getFromName("SQL/sql.xml")); // Init tmp vars $strTableName = ""; $arrFieldList = array(); $arrDefList = array(); $strOptions = array(); while ($xmlReader->read()) { switch ($xmlReader->nodeType) { case XMLReader::ELEMENT: switch ($xmlReader->localName) { case "table": $strTableName = $xmlReader->getAttribute("name"); $arrFieldList = array(); $arrDefList = array(); break; case "field": $arrFieldList[$xmlReader->getAttribute("name")] = $xmlReader->readString(); break; case "def": $arrDefList[$xmlReader->getAttribute("name")] = $xmlReader->readString(); break; case "option": $strOptions = $xmlReader->readString(); break; } break; case XMLReader::END_ELEMENT: switch ($xmlReader->localName) { case "table": $mixResult = $this->compareDatabase($strTableName, $arrFieldList, $arrDefList, $strOptions); if ($mixResult !== true && is_array($mixResult)) { $arrError = array_merge($arrError, $mixResult); } break; } break; } } if (count($arrError) == 0) { return true; } else { return $arrError; } }
protected function runRestoreFromSer($strRestoreFile) { $objZipArchive = new ZipArchiveCto(); $objTempfile = tmpfile(); $arrRestoreTables = array(); try { // Open ZIP Archive $objZipArchive->open($strRestoreFile); // Get structure if ($objZipArchive->locateName($this->strFilenameTable) === false) { throw new Exception("Could not load SQL file table. Maybe damaged?"); } $mixTables = $objZipArchive->getFromName($this->strFilenameTable); $mixTables = trimsplit("\n", $mixTables); // Create temp tables foreach ($mixTables as $key => $value) { if (empty($value)) { continue; } $value = unserialize($value); if (!is_array($value)) { throw new Exception("Could not load SQL file table. Maybe damaged?"); } $this->Database->query("DROP TABLE IF EXISTS " . "synccto_temp_" . $value["name"]); $this->Database->query($this->buildSQLTable($value["value"], "synccto_temp_" . $value["name"])); $arrRestoreTables[] = $value["name"]; } // Get insert if ($objZipArchive->locateName($this->strFilenameInsert) === false) { throw new Exception("Could not load SQL file inserts. Maybe damaged?"); } $strContent = $objZipArchive->getFromName($this->strFilenameInsert); // Write temp File fputs($objTempfile, $strContent, strlen($strContent)); unset($strContent); // Set pointer on position zero rewind($objTempfile); $i = 0; while ($mixLine = fgets($objTempfile)) { $i++; if (empty($mixLine) || strlen($mixLine) == 0) { continue; } $mixLine = json_decode(@gzuncompress(base64_decode($mixLine)), true); if ($mixLine == FALSE) { throw new Exception("Could not load SQL file inserts or unzip it. Maybe damaged on line {$i}?"); } if (!is_array($mixLine)) { throw new Exception("Could not load SQL file inserts. Maybe damaged on line {$i}?"); } $strSQL = $this->buildSQLInsert("synccto_temp_" . $mixLine['table'], array_keys($mixLine['values']), $mixLine['values'], true); $this->Database->query($strSQL); } $objZipArchive->close(); fclose($objTempfile); return $arrRestoreTables; } catch (Exception $exc) { foreach ($arrRestoreTables as $key => $value) { $this->Database->query("DROP TABLE IF EXISTS " . "synccto_temp_" . $value); } $objZipArchive->close(); fclose($objTempfile); throw $exc; } }