Server sync
This commit is contained in:
81
inc/catc.php
81
inc/catc.php
@@ -2,6 +2,9 @@
|
||||
|
||||
class CATC extends Main
|
||||
{
|
||||
|
||||
const GZ_LVL = 4;
|
||||
|
||||
/**
|
||||
* Auth Object
|
||||
* @var Auth
|
||||
@@ -21,6 +24,9 @@ class CATC extends Main
|
||||
|
||||
//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);
|
||||
|
||||
//TODO remove. For upgrade purposes only
|
||||
(new Doc($this->oDb))->getList('full');
|
||||
}
|
||||
|
||||
protected function install()
|
||||
@@ -41,7 +47,7 @@ class CATC extends Main
|
||||
Course::COURSE_TABLE => array(Db::getId('workshops'), 'description', 'timeslot'),
|
||||
Note::NOTE_TABLE => array(Db::getId(Auth::USER_TABLE), Db::getId(Course::COURSE_TABLE), 'notes'),
|
||||
Definition::DEF_TABLE => array(Db::getId(Auth::USER_TABLE), 'title', 'description'),
|
||||
Doc::DOC_TABLE => array(Db::getId(Auth::USER_TABLE), Db::getId(Course::COURSE_TABLE), 'type', 'filename'),
|
||||
Doc::DOC_TABLE => array(Db::getId(Auth::USER_TABLE), Db::getId(Course::COURSE_TABLE), 'type', 'filename', 'filehash'),
|
||||
'todos' => array(Db::getId(Auth::USER_TABLE), Db::getId(Course::COURSE_TABLE), 'description')
|
||||
),
|
||||
'types' => array
|
||||
@@ -56,7 +62,8 @@ class CATC extends Main
|
||||
'timeslot' => "ENUM('SAT-M', 'SAT-A', 'SUN-M', 'SUN-A')",
|
||||
'notes' => "LONGTEXT",
|
||||
'type' => "VARCHAR(10)",
|
||||
'filename' => "VARCHAR(200)"
|
||||
'filename' => "VARCHAR(200)",
|
||||
'filehash' => "VARCHAR(40)"
|
||||
),
|
||||
'constraints' => array
|
||||
(
|
||||
@@ -181,4 +188,74 @@ class CATC extends Main
|
||||
$bResult = $oDef->setDefinition($sTitle, $sDesc);
|
||||
return self::getJsonResult($bResult, '', array('new_def'=>$bNew, 'def'=>$oDef->getDefinition()));
|
||||
}
|
||||
|
||||
/* Sync */
|
||||
|
||||
public function pushToServer() {
|
||||
$bSuccess = false;
|
||||
$sDesc = '';
|
||||
|
||||
if(Settings::SERVER_URL == '') $sDesc = 'No remote server configured';
|
||||
else {
|
||||
$sBackup = $this->oDb->getBackup();
|
||||
if($sBackup === false) $sDesc = 'Error executing mysqldump';
|
||||
else {
|
||||
//Store backup as a zip file
|
||||
$sBackupPath = Doc::DOC_FOLDER.'db/'.uniqid('backup_').'.sql.gz';
|
||||
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'];
|
||||
unlink($sBackupPath);
|
||||
|
||||
//Send missing files
|
||||
$asFiles = $asResult['data']['files'];
|
||||
foreach($asFiles as $asFile) {
|
||||
$this->sendFileToServer('file_update', $asFile['filepath'], $asFile['filename']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return self::getJsonResult($bSuccess, $sDesc);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
//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);
|
||||
|
||||
//TODO Check data integrity
|
||||
}
|
||||
|
||||
private static function sendFileToServer($sAction, $sFilePath, $sFileName='file') {
|
||||
$asPostData = array('a'=>$sAction, 'api'=>Settings::API_KEY);
|
||||
$asPostData['file'] = new CURLFile(realpath($sFilePath), mime_content_type($sFilePath), $sFileName);
|
||||
return ToolBox::curl(Settings::SERVER_URL, false, $asPostData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user