Sync server v2

This commit is contained in:
2019-10-07 18:01:03 +02:00
parent 76346ecce8
commit b40d3f7783
2 changed files with 76 additions and 37 deletions

View File

@@ -20,7 +20,7 @@ class CATC extends Main
array('name'=>'doc', 'project'=>true), array('name'=>'doc', 'project'=>true),
array('name'=>'definition', '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); //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); $this->oAuth = new Auth($this->oDb, Settings::API_KEY);
@@ -199,66 +199,105 @@ class CATC extends Main
if($sBackup === false) $sDesc = 'Error executing mysqldump'; if($sBackup === false) $sDesc = 'Error executing mysqldump';
else { else {
//Store backup as a zip file //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)); file_put_contents($sBackupPath, gzencode($sBackup, self::GZ_LVL));
//Send backup //Send backup
$sResult = self::sendFileToServer('server_update', $sBackupPath); $asResult = self::sendFilesToServer('server_update', array('filepath'=>$sBackupPath, 'filename'=>$sBackupFile));
//Parse response from server
$asResult = json_decode($sResult, true);
$bSuccess = ($asResult['result'] == self::SUCCESS);
$sDesc = $asResult['desc'];
unlink($sBackupPath); unlink($sBackupPath);
//Send missing files if($asResult['result']) {
$asFiles = $asResult['data']['files']; $asData = $asResult['content'];
foreach($asFiles as $asFile) { $bSuccess = ($asData['result'] == self::SUCCESS);
$this->sendFileToServer('file_update', $asFile['filepath'], $asFile['filename']); $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); 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() { public function updateServer() {
$bSuccess = false; $bSuccess = false;
$sDesc = ''; $sDesc = '';
$asMissingFiles = array(); $asMissingFiles = array();
//Replace DB //Replace DB
$sBackupPath = Doc::DOC_FOLDER.'db/'.uniqid('backup_').'.sql'; $asFilePaths = $this->updateFiles(Doc::DOC_FOLDER.'db/', true);
$sCompressedPath = $sBackupPath.'.gz'; if(empty($asFilePaths)) $sDesc = 'Missing db file on remote server';
move_uploaded_file($_FILES['file']['tmp_name'], $sCompressedPath); else {
file_put_contents($sBackupPath, gzdecode(file_get_contents($sCompressedPath))); $sCompressedPath = array_shift($asFilePaths);
unlink($sCompressedPath); $sBackupPath = str_replace('.gz', '', $sCompressedPath);
$sDesc = $this->oDb->restoreBackup($sBackupPath); file_put_contents($sBackupPath, gzdecode(file_get_contents($sCompressedPath)));
$bSuccess = ($sDesc==''); unlink($sCompressedPath);
unlink($sBackupPath); $sDesc = $this->oDb->restoreBackup($sBackupPath);
$bSuccess = ($sDesc=='');
unlink($sBackupPath);
if($bSuccess) { if($bSuccess) {
//Reset passwords //Reset passwords
$this->oAuth->resetPass(); $this->oAuth->resetPass();
//Check for missing files //Check for missing files
$asMissingFiles = (new Doc($this->oDb))->getMissingFiles(); $asMissingFiles = (new Doc($this->oDb))->getMissingFiles();
}
} }
//Send list of missing files back //Send list of missing files back
return self::getJsonResult($bSuccess, $sDesc, array('files'=>$asMissingFiles)); return self::getJsonResult($bSuccess, $sDesc, array('files'=>$asMissingFiles));
} }
public function updateFile($sPath='') { public function updateFiles($sFolder=Doc::DOC_FOLDER, $bInternal=false) {
if($sPath=='') $sPath = Doc::DOC_FOLDER.$_FILES['file']['name']; $bSuccess = false;
move_uploaded_file($_FILES['file']['tmp_name'], $sPath); $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 //TODO Check data integrity
} }
$bSuccess = true;
}
else $sDesc = 'No file to update';
private static function sendFileToServer($sAction, $sFilePath, $sFileName='file') { return $bInternal?$asFilePaths:self::getJsonResult($bSuccess, $sDesc);
$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);
} }
} }

View File

@@ -88,7 +88,7 @@ elseif($sAction!='' && !$bLoggedIn)
$sResult = $oCATC->updateServer(); $sResult = $oCATC->updateServer();
break; break;
case 'file_update': case 'file_update':
$sResult = $oCATC->updateFile(); $sResult = $oCATC->updateFiles();
break; break;
default: default:
$sResult = CATC::getJsonResult(false, CATC::NOT_FOUND); $sResult = CATC::getJsonResult(false, CATC::NOT_FOUND);