Example #1
0
 /**
  * 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;
     }
 }
Example #2
0
 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;
     }
 }