140 lines
2.9 KiB
PHP
140 lines
2.9 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: lucas
|
|
* Date: 15/03/18
|
|
* Time: 15:50
|
|
*/
|
|
|
|
namespace api\module\department;
|
|
|
|
|
|
use database\core\Repo;
|
|
use database\repo\department;
|
|
use Ifsnop\Mysqldump\Mysqldump;
|
|
|
|
class saveController
|
|
{
|
|
|
|
private $backupPath = "";
|
|
private $originDBName = "";
|
|
|
|
private function initDir(string $dbName){
|
|
|
|
//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;
|
|
|
|
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];
|
|
}
|
|
|
|
public function delete($args){
|
|
$this->initDir($_SESSION["CurrentDatabase"]);
|
|
|
|
$backupName = "";
|
|
|
|
extract($args);
|
|
|
|
return ["success" => unlink($this->backupPath.$backupName.".sql")];
|
|
}
|
|
|
|
public function post($args){
|
|
$this->initDir($_SESSION["CurrentDatabase"]);
|
|
|
|
$backupName = "";
|
|
$apply = false;
|
|
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();
|
|
|
|
/** @var department $depRepo */
|
|
$depRepo = Repo::getRepo("department");
|
|
|
|
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']];
|
|
|
|
}
|
|
}
|
|
|
|
} |