2018-03-15 15:47:22 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: lucas
|
|
|
|
* Date: 15/03/18
|
|
|
|
* Time: 15:50
|
|
|
|
*/
|
|
|
|
|
2018-03-15 17:18:48 +00:00
|
|
|
namespace api\module\department;
|
2018-03-15 15:47:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
use database\core\Repo;
|
2018-03-15 17:18:48 +00:00
|
|
|
use database\repo\department;
|
2018-03-15 15:47:22 +00:00
|
|
|
use Ifsnop\Mysqldump\Mysqldump;
|
|
|
|
|
|
|
|
class saveController
|
|
|
|
{
|
|
|
|
|
|
|
|
private $backupPath = "";
|
2018-03-16 19:40:04 +00:00
|
|
|
private $originDBName = "";
|
2018-03-15 15:47:22 +00:00
|
|
|
|
|
|
|
private function initDir(string $dbName){
|
|
|
|
|
2018-03-16 19:40:04 +00:00
|
|
|
//match preview_DATABASENAME_sha1 in order to determine if we init the directory or not
|
|
|
|
$matches = [];
|
|
|
|
$reg = preg_match("/preview_(\w*)_\w*/",$dbName,$matches);
|
|
|
|
|
|
|
|
//if the dbname match we store the original database name
|
|
|
|
if($reg == 1){
|
|
|
|
$dbName = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->originDBName = $dbName;
|
|
|
|
|
2018-03-15 15:47:22 +00:00
|
|
|
if(!is_dir(__BACKUP__."/$dbName/")){
|
|
|
|
mkdir(__BACKUP__."/$dbName/");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->backupPath =__BACKUP__."/$dbName/";
|
|
|
|
}
|
|
|
|
|
|
|
|
private function scandir(string $path) : array {
|
|
|
|
//scan the directory
|
|
|
|
$arr = scandir($path);
|
|
|
|
|
|
|
|
//strip the useless "." and ".."
|
|
|
|
unset($arr[0],$arr[1]);
|
|
|
|
|
|
|
|
//make the arry start at 0 again
|
|
|
|
$arr = array_values($arr);
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get($args){
|
|
|
|
$this->initDir($_SESSION["CurrentDatabase"]);
|
|
|
|
|
|
|
|
//strip extensions
|
|
|
|
$backupNames = array_map(function($e){
|
|
|
|
return pathinfo($e, PATHINFO_FILENAME);
|
|
|
|
}, $this->scandir($this->backupPath));
|
|
|
|
|
|
|
|
return ["data" => $backupNames];
|
|
|
|
}
|
|
|
|
|
2018-03-16 19:40:04 +00:00
|
|
|
public function delete($args){
|
|
|
|
$this->initDir($_SESSION["CurrentDatabase"]);
|
|
|
|
|
|
|
|
$backupName = "";
|
|
|
|
|
|
|
|
extract($args);
|
|
|
|
|
|
|
|
return ["success" => unlink($this->backupPath.$backupName.".sql")];
|
|
|
|
}
|
|
|
|
|
2018-03-15 15:47:22 +00:00
|
|
|
public function post($args){
|
|
|
|
$this->initDir($_SESSION["CurrentDatabase"]);
|
|
|
|
|
|
|
|
$backupName = "";
|
2018-03-16 19:40:04 +00:00
|
|
|
$apply = false;
|
2018-03-15 15:47:22 +00:00
|
|
|
extract($args);
|
|
|
|
|
|
|
|
//if the backup name is empty we create it
|
|
|
|
if($backupName == ""){
|
|
|
|
try {
|
|
|
|
$conf = Repo::getDBConfig();
|
|
|
|
|
|
|
|
$dump = new Mysqldump("mysql:host={$conf["host"]};dbname={$conf["dbname"]}", $conf["username"], $conf["password"],
|
|
|
|
[
|
|
|
|
"compress" => Mysqldump::GZIP
|
|
|
|
]);
|
|
|
|
|
|
|
|
$date = date("Y-W-d");
|
|
|
|
|
|
|
|
$dump->start($this->backupPath.$date.".sql");
|
|
|
|
|
|
|
|
return ["success" => true,"backupName" => $date];
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return ["success" => false];
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
//read the backup
|
|
|
|
ob_start();
|
|
|
|
readgzfile($this->backupPath.$backupName.".sql");
|
|
|
|
$sql = ob_get_clean();
|
|
|
|
|
2018-03-15 17:18:48 +00:00
|
|
|
/** @var department $depRepo */
|
|
|
|
$depRepo = Repo::getRepo("department");
|
2018-03-15 15:47:22 +00:00
|
|
|
|
2018-03-16 19:40:04 +00:00
|
|
|
if($sql == "") return ["success" => false];
|
|
|
|
|
|
|
|
if($apply){
|
|
|
|
|
|
|
|
$depRepo->restore($this->originDBName,$sql);
|
|
|
|
|
|
|
|
return ["success" => true];
|
|
|
|
}else{
|
|
|
|
|
|
|
|
if($backupName == "origin"){
|
|
|
|
$_SESSION['CurrentDatabase'] = $this->originDBName;
|
|
|
|
return ["success" => true];
|
|
|
|
}
|
|
|
|
|
|
|
|
$previewDBName = $depRepo->previewExists($this->originDBName,$backupName);
|
|
|
|
|
|
|
|
if($previewDBName == null){
|
|
|
|
$previewDBName = $depRepo->createPreview($this->originDBName,$backupName);
|
|
|
|
$depRepo->restore($previewDBName,$sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
$_SESSION['CurrentDatabase'] = $previewDBName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ["success" => true, "currentDatabase" => $_SESSION['CurrentDatabase']];
|
2018-03-15 15:47:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|