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']]; } } }