From b40d3f77831660a14ebb92b14ba6d045ca1fb294 Mon Sep 17 00:00:00 2001 From: Franzz Date: Mon, 7 Oct 2019 18:01:03 +0200 Subject: [PATCH] Sync server v2 --- inc/catc.php | 111 ++++++++++++++++++++++++++++++++++----------------- index.php | 2 +- 2 files changed, 76 insertions(+), 37 deletions(-) diff --git a/inc/catc.php b/inc/catc.php index 4e5986a..020b0c3 100644 --- a/inc/catc.php +++ b/inc/catc.php @@ -20,7 +20,7 @@ class CATC extends Main array('name'=>'doc', 'project'=>true), array('name'=>'definition', 'project'=>true) ); - parent::__construct($oClassManagement, $sProcessPage, $asClasses); + parent::__construct($oClassManagement, $sProcessPage, $asClasses, true , __FILE__); //if($this->oDb->sDbState == Db::DB_PEACHY) $this->oAuth = new Auth($this->oDb, Settings::API_KEY); $this->oAuth = new Auth($this->oDb, Settings::API_KEY); @@ -199,66 +199,105 @@ class CATC extends Main if($sBackup === false) $sDesc = 'Error executing mysqldump'; else { //Store backup as a zip file - $sBackupPath = Doc::DOC_FOLDER.'db/'.uniqid('backup_').'.sql.gz'; + $sBackupFile = uniqid('backup_').'.sql.gz'; + $sBackupPath = Doc::DOC_FOLDER.'db/'.$sBackupFile; file_put_contents($sBackupPath, gzencode($sBackup, self::GZ_LVL)); //Send backup - $sResult = self::sendFileToServer('server_update', $sBackupPath); - - //Parse response from server - $asResult = json_decode($sResult, true); - $bSuccess = ($asResult['result'] == self::SUCCESS); - $sDesc = $asResult['desc']; + $asResult = self::sendFilesToServer('server_update', array('filepath'=>$sBackupPath, 'filename'=>$sBackupFile)); unlink($sBackupPath); - //Send missing files - $asFiles = $asResult['data']['files']; - foreach($asFiles as $asFile) { - $this->sendFileToServer('file_update', $asFile['filepath'], $asFile['filename']); + if($asResult['result']) { + $asData = $asResult['content']; + $bSuccess = ($asData['result'] == self::SUCCESS); + $sDesc = $asData['desc']; + + //Send missing files + if($bSuccess && isset($asData['data']['files']) && !empty($asData['data']['files'])) { + $asResult = $this->sendFilesToServer('file_update', $asData['data']['files']); + if($asResult['result']) { + $asData = $asResult['content']; + $bSuccess = ($asData['result'] == self::SUCCESS); + $sDesc = $asData['desc']; + } + else $sDesc = $asResult['desc']; + } + else $this->addNotice('No missing files identified'); } + else $sDesc = $asResult['desc']; } } return self::getJsonResult($bSuccess, $sDesc); } + private static function sendFilesToServer($sAction, $asFiles) { + if(is_string($asFiles)) $asFiles = array(array('filepath'=>$asFiles, 'filename'=>'file')); + elseif(array_key_exists('filepath', $asFiles)) $asFiles = array($asFiles); + + $asPostData = array('a'=>$sAction, 'api'=>Settings::SERVER_KEY); + $asMoves = array(); + foreach($asFiles as $iIndex=>$asFile) { + $asPathInfo = pathinfo($asFile['filepath']); + $asMove = array('origin'=>$asFile['filepath'], 'safe'=>$asPathInfo['dirname'].'/'.uniqid('file_').'.'.$asPathInfo['extension']); + rename($asMove['origin'], $asMove['safe']); + $asPostData['files['.$iIndex.']'] = new CURLFile(realpath($asMove['safe']), mime_content_type($asMove['safe']), $asFile['filename']); + $asMoves[] = $asMove; + } + + $asResult = ToolBox::curl(Settings::SERVER_URL, false, $asPostData, 'json'); + foreach($asMoves as $asMove) rename($asMove['safe'], $asMove['origin']); + + return $asResult; + } + public function updateServer() { $bSuccess = false; $sDesc = ''; $asMissingFiles = array(); //Replace DB - $sBackupPath = Doc::DOC_FOLDER.'db/'.uniqid('backup_').'.sql'; - $sCompressedPath = $sBackupPath.'.gz'; - move_uploaded_file($_FILES['file']['tmp_name'], $sCompressedPath); - file_put_contents($sBackupPath, gzdecode(file_get_contents($sCompressedPath))); - unlink($sCompressedPath); - $sDesc = $this->oDb->restoreBackup($sBackupPath); - $bSuccess = ($sDesc==''); - unlink($sBackupPath); - - if($bSuccess) { - //Reset passwords - $this->oAuth->resetPass(); + $asFilePaths = $this->updateFiles(Doc::DOC_FOLDER.'db/', true); + if(empty($asFilePaths)) $sDesc = 'Missing db file on remote server'; + else { + $sCompressedPath = array_shift($asFilePaths); + $sBackupPath = str_replace('.gz', '', $sCompressedPath); + file_put_contents($sBackupPath, gzdecode(file_get_contents($sCompressedPath))); + unlink($sCompressedPath); + $sDesc = $this->oDb->restoreBackup($sBackupPath); + $bSuccess = ($sDesc==''); + unlink($sBackupPath); - //Check for missing files - $asMissingFiles = (new Doc($this->oDb))->getMissingFiles(); + if($bSuccess) { + //Reset passwords + $this->oAuth->resetPass(); + + //Check for missing files + $asMissingFiles = (new Doc($this->oDb))->getMissingFiles(); + } } //Send list of missing files back return self::getJsonResult($bSuccess, $sDesc, array('files'=>$asMissingFiles)); } - public function updateFile($sPath='') { - if($sPath=='') $sPath = Doc::DOC_FOLDER.$_FILES['file']['name']; - move_uploaded_file($_FILES['file']['tmp_name'], $sPath); + public function updateFiles($sFolder=Doc::DOC_FOLDER, $bInternal=false) { + $bSuccess = false; + $sDesc = ''; + $asFilePaths = array(); + if(!empty($_FILES)) { + $asFiles = $_FILES['files']; + for($iIndex=0 ; $iIndex < count($asFiles['name']) ; $iIndex++) { + $sFilePath = $sFolder.$asFiles['name'][$iIndex]; + move_uploaded_file($asFiles['tmp_name'][$iIndex], $sFilePath); + $asFilePaths[] = $sFilePath; + + //TODO Check data integrity + } + $bSuccess = true; + } + else $sDesc = 'No file to update'; - //TODO Check data integrity - } - - private static function sendFileToServer($sAction, $sFilePath, $sFileName='file') { - $asPostData = array('a'=>$sAction, 'api'=>Settings::SERVER_KEY); - $asPostData['file'] = new CURLFile(realpath($sFilePath), mime_content_type($sFilePath), $sFileName); - return ToolBox::curl(Settings::SERVER_URL, false, $asPostData); + return $bInternal?$asFilePaths:self::getJsonResult($bSuccess, $sDesc); } } \ No newline at end of file diff --git a/index.php b/index.php index 59d87d6..842d5f5 100644 --- a/index.php +++ b/index.php @@ -88,7 +88,7 @@ elseif($sAction!='' && !$bLoggedIn) $sResult = $oCATC->updateServer(); break; case 'file_update': - $sResult = $oCATC->updateFile(); + $sResult = $oCATC->updateFiles(); break; default: $sResult = CATC::getJsonResult(false, CATC::NOT_FOUND);