Server sync
This commit is contained in:
1
files/db/update_v3_to_v4.sql
Normal file
1
files/db/update_v3_to_v4.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE docs ADD filehash varchar(40) AFTER filename;
|
||||
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);
|
||||
}
|
||||
}
|
||||
46
inc/doc.php
46
inc/doc.php
@@ -52,7 +52,7 @@ class Doc extends PhpObject {
|
||||
}
|
||||
}
|
||||
|
||||
$asData = array_merge($this->getDocKeys(), array('filename'=>$sFileName, 'type'=>$sType));
|
||||
$asData = array_merge($this->getDocKeys(), array('type'=>$sType, 'filename'=>$sFileName, 'filehash'=>self::getChecksum($sFileName)));
|
||||
$bResult = $this->oDb->insertRow(self::DOC_TABLE, $asData);
|
||||
|
||||
return $bResult?'':'error_db';
|
||||
@@ -73,18 +73,54 @@ class Doc extends PhpObject {
|
||||
return array_shift($asDocList);
|
||||
}
|
||||
|
||||
public function getList($asConstraint=array()) {
|
||||
$asKeys = empty($asConstraint)?$this->getDocKeys():$asConstraint;
|
||||
$asDocs = $this->oDb->selectRows(array('select'=>array(Db::getId(self::DOC_TABLE), 'type', 'filename'), 'from'=>self::DOC_TABLE, 'constraint'=>$asKeys));
|
||||
foreach($asDocs as &$asDoc) $asDoc['filepath'] = self::getFilePath($asDoc['filename']);
|
||||
public function getList($oOption='self') {
|
||||
if($oOption=='self') $asKeys = $this->getDocKeys();
|
||||
elseif($oOption=='full') $asKeys = array();
|
||||
elseif(is_array($oOption)) $asKeys = $oOption;
|
||||
else return array();
|
||||
|
||||
$asDocs = $this->oDb->selectRows(array(
|
||||
'select' => array(Db::getId(self::DOC_TABLE), 'type', 'filename', 'filehash'),
|
||||
'from' => self::DOC_TABLE,
|
||||
'constraint'=> $asKeys
|
||||
));
|
||||
|
||||
foreach($asDocs as &$asDoc) {
|
||||
$asDoc['filepath'] = self::getFilePath($asDoc['filename']);
|
||||
|
||||
//TODO remove. For upgrade purposes only
|
||||
if($asDoc['filehash']=='') {
|
||||
$asDoc['filehash'] = self::getChecksum($asDoc['filename']);
|
||||
$this->oDb->updateRow(self::DOC_TABLE, $asDoc[Db::getId(self::DOC_TABLE)], array('filehash'=>$asDoc['filehash']), false);
|
||||
}
|
||||
}
|
||||
|
||||
return $asDocs;
|
||||
}
|
||||
|
||||
public function getMissingFiles() {
|
||||
$asDocs = $this->getList('full');
|
||||
$asMissingFiles = array();
|
||||
foreach($asDocs as $asDoc) {
|
||||
$sHash = self::getChecksum($asDoc['filename']);
|
||||
if(!file_exists($asDoc['filepath']) || $sHash!=$asDoc['filehash']) {
|
||||
$asDoc['actual_hash'] = $sHash;
|
||||
$asMissingFiles[] = $asDoc;
|
||||
}
|
||||
}
|
||||
return $asMissingFiles;
|
||||
}
|
||||
|
||||
private function getDocKeys() {
|
||||
return array(Db::getId(Auth::USER_TABLE) => $this->iUserId, Db::getId(Course::WS_TABLE) => $this->iWorkshopId);
|
||||
}
|
||||
|
||||
private static function getChecksum($sFileName) {
|
||||
$sFilePath = self::getFilePath($sFileName);
|
||||
return file_exists($sFilePath)?sha1_file($sFilePath):'';
|
||||
|
||||
}
|
||||
|
||||
private static function getFilePath($sFileName) {
|
||||
return self::DOC_FOLDER.$sFileName;
|
||||
}
|
||||
|
||||
13
index.php
13
index.php
@@ -3,7 +3,7 @@
|
||||
/*
|
||||
CATC Project
|
||||
https://git.lutran.fr/franzz/catc
|
||||
Copyright (C) 2015 François Lutran
|
||||
Copyright (C) 2019 François Lutran
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -34,7 +34,7 @@ ToolBox::cleanPost($_REQUEST);
|
||||
$sToken = isset($_REQUEST['token'])?$_REQUEST['token']:'';
|
||||
$sAction = isset($_REQUEST['a'])?$_REQUEST['a']:'';
|
||||
$sNickName = isset($_REQUEST['nickname'])?$_REQUEST['nickname']:'';
|
||||
$iApiKey = isset($_GET['api'])?$_GET['api']:'';
|
||||
$iApiKey = isset($_REQUEST['api'])?$_REQUEST['api']:'';
|
||||
$sContent = isset($_POST['content'])?$_POST['content']:'';
|
||||
$iId = isset($_REQUEST['id'])?$_REQUEST['id']:0;
|
||||
$sTitle = isset($_REQUEST['title'])?$_REQUEST['title']:'';
|
||||
@@ -71,6 +71,9 @@ elseif($sAction!='' && $bLoggedIn)
|
||||
case 'set_def':
|
||||
$sResult = $oCATC->setDef($iId, $sTitle, $sDesc);
|
||||
break;
|
||||
case 'server_push': //Sends db to server
|
||||
$sResult = $oCATC->pushToServer();
|
||||
break;
|
||||
default:
|
||||
$sResult = CATC::getJsonResult(false, CATC::NOT_FOUND);
|
||||
}
|
||||
@@ -81,6 +84,12 @@ elseif($sAction!='' && !$bLoggedIn)
|
||||
{
|
||||
switch ($sAction)
|
||||
{
|
||||
case 'server_update': //update db with sent data
|
||||
$sResult = $oCATC->updateServer();
|
||||
break;
|
||||
case 'file_update':
|
||||
$sResult = $oCATC->updateFile();
|
||||
break;
|
||||
default:
|
||||
$sResult = CATC::getJsonResult(false, CATC::NOT_FOUND);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<a tabindex="0" class="body btn btn-outline-primary ml-2 my-0" role="button" data-toggle="popover"><i class="fa-body"></i></a>
|
||||
<button class="sync btn btn-outline-primary ml-2 my-0" type="button"><i class="fa-sync"></i></button>
|
||||
<button class="home btn btn-outline-primary ml-2 my-0" type="button"><i class="fa-home"></i></button>
|
||||
<button class="logoff btn btn-outline-primary ml-2 my-0" type="button" data-toggle="modal" data-target="#log-off"><i class="fa-logoff"></i></button>
|
||||
</form>
|
||||
|
||||
@@ -192,6 +192,17 @@ function CATC(asGlobals)
|
||||
$Modal.find('.body-title').text(sTitle);
|
||||
});
|
||||
|
||||
//Sync
|
||||
self.elem.$Menu.find('.sync').click(function(){
|
||||
Tools.ajax(
|
||||
'server_push',
|
||||
()=>{self.feedback('success', 'Successful Sync');},
|
||||
{},
|
||||
()=>{self.feedback('error', 'Sync failed');},
|
||||
$(this).find('i')
|
||||
);
|
||||
});
|
||||
|
||||
//Home
|
||||
self.elem.$Menu.find('.home').click(function(){self.setHash(self.consts.default_page);});
|
||||
|
||||
|
||||
@@ -22,14 +22,13 @@ var Tools = {
|
||||
else asData = oData;
|
||||
asData['a'] = sAction;
|
||||
|
||||
if($Loader)
|
||||
{
|
||||
if($Loader) {
|
||||
$Loader
|
||||
.data('load_html', $Loader.html())
|
||||
.data('load_class', $Loader.attr('class'))
|
||||
.removeClassPrefix('fa-')
|
||||
.addClass('loader')
|
||||
.html($('<img>', {'class':'onlyimg', src: Config.paths.dir.theme_image+"ajax.svg"}));
|
||||
.addClass('fa-loading')
|
||||
.html('');
|
||||
}
|
||||
|
||||
if(bBeacon) {
|
||||
@@ -253,6 +252,16 @@ $.fn.toPx = function(settings){
|
||||
return Math.round(that * scopeVal) + 'px';
|
||||
};
|
||||
|
||||
$.fn.removeClassPrefix = function(prefix) {
|
||||
this.each(function(i, el) {
|
||||
var classes = el.className.split(" ").filter(function(c) {
|
||||
return c.lastIndexOf(prefix, 0) !== 0;
|
||||
});
|
||||
el.className = $.trim(classes.join(" "));
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
function getElem(anchor, path)
|
||||
{
|
||||
return (typeof path == 'object' && path.length > 1)?getElem(anchor[path.shift()], path):anchor[(typeof path == 'object')?path.shift():path];
|
||||
|
||||
@@ -11,5 +11,7 @@ class Settings
|
||||
const TIMEZONE = 'Pacific/Auckland';
|
||||
const API_KEY = '';
|
||||
const RAND_TEXT = "let's_mess%a&bit;with~it,!just§for¨the^sake*of-it";
|
||||
const SERVER_URL = '';
|
||||
const SERVER_KEY = '';
|
||||
const DEBUG = true; //prod: false, dev: true
|
||||
}
|
||||
@@ -23,7 +23,7 @@ $fa-css-prefix: fa;
|
||||
.#{$fa-css-prefix}-add:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-plus); }
|
||||
.#{$fa-css-prefix}-edit:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-pencil); }
|
||||
.#{$fa-css-prefix}-delete:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-trash-alt); }
|
||||
.#{$fa-css-prefix}-loading:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-spinner-third); }
|
||||
.#{$fa-css-prefix}-loading:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-spinner-third); @extend .#{$fa-css-prefix}-spin;}
|
||||
|
||||
.#{$fa-css-prefix}-danger:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-exclamation-square); }
|
||||
.#{$fa-css-prefix}-warning:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-exclamation-triangle); }
|
||||
@@ -36,6 +36,7 @@ $fa-css-prefix: fa;
|
||||
//Menu
|
||||
.#{$fa-css-prefix}-search:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-search); }
|
||||
.#{$fa-css-prefix}-body:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-child); }
|
||||
.#{$fa-css-prefix}-sync:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-cloud-upload); }
|
||||
.#{$fa-css-prefix}-home:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-home); }
|
||||
.#{$fa-css-prefix}-logoff:before {@extend %fa-icon; @extend .fal; content: fa-content($fa-var-sign-out-alt);}
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user