/** * Create a xml file with table informations */ protected function addDatabase() { // Create XML File $objXml = new XMLWriter(); $objXml->openMemory(); $objXml->setIndent(true); $objXml->setIndentString("\t"); // XML Start $objXml->startDocument('1.0', 'UTF-8'); $objXml->startElement('database'); // Write meta (header) $objXml->startElement('metatags'); $objXml->writeElement('version', $GLOBALS['SYC_VERSION']); $objXml->writeElement('create_unix', time()); $objXml->writeElement('create_date', date('Y-m-d', time())); $objXml->writeElement('create_time', date('H:i', time())); $objXml->endElement(); // End metatags $objXml->startElement('structure'); foreach ($this->arrTables as $TableName) { // Get data $arrStructure = $this->objSyncCtoDatabase->getTableStructure($TableName); // Check if empty if (count($arrStructure) == 0) { continue; } $objXml->startElement('table'); $objXml->writeAttribute("name", $TableName); $objXml->startElement('fields'); if (is_array($arrStructure['TABLE_FIELDS'])) { foreach ($arrStructure['TABLE_FIELDS'] as $keyField => $valueField) { $objXml->startElement('field'); $objXml->writeAttribute("name", $keyField); $objXml->text($valueField); $objXml->endElement(); // End field } } $objXml->endElement(); // End fields $objXml->startElement('definitions'); if (is_array($arrStructure['TABLE_CREATE_DEFINITIONS'])) { foreach ($arrStructure['TABLE_CREATE_DEFINITIONS'] as $keyField => $valueField) { $objXml->startElement('def'); $objXml->writeAttribute("name", $keyField); $objXml->text($valueField); $objXml->endElement(); // End field } } $objXml->endElement(); // End fields $objXml->startElement("option"); $objXml->text($arrStructure['TABLE_OPTIONS']); $objXml->endElement(); $objXml->endElement(); // End table } $objXml->endElement(); // End structure $objXml->endElement(); // End database $this->objZipArchive->addFromString("SQL/sql.xml", $objXml->flush(true)); }
/** * 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; } }