<?php

class backup
{
    public static function create()
    {
        try {
            $nameNewFolder = date("Ymd-His");
            $newFolder = DIR_TEMP . $nameNewFolder;
            mkdir($newFolder, 0777);
            self::dumpMysql($newFolder);
            file::copyFolder(DIR_DATAS, $newFolder . "/datas");
            $localZip = file::zip($newFolder, DIR_TEMP . $nameNewFolder);
            file::deleteFolder($newFolder);
            rename($localZip, DIR_BACKUP . $nameNewFolder . ".zip");
            alert::recSuccess("BackUp OK :. " . $nameNewFolder . ".zip");
            return TRUE;
        } catch (Exception $ex) {
            alert::recError("BackUp :. Erreur lors de la création");
            return FALSE;
        }
    }

    public static function restore(string $_zip)
    {
        $tmpZip = str_replace(DIR_BACKUP, DIR_TEMP, $_zip);
        $tmpDatas = date("Ymd-His") . "-datas";
        copy($_zip, $tmpZip); 
        $folder = file::unzip($tmpZip, DIR_TEMP); 
        if(self::dumpMysqlRestore(DIR_TEMP . $folder . "/" . DB_NAME . ".Mysql")){
            rename(DIR_DATAS, DIR_TEMP . $tmpDatas);
            file::copyFolder(DIR_TEMP . $folder . "/datas", DIR_DATAS);
            if(is_dir(DIR_DATAS)){
                file::deleteFolder(DIR_TEMP . $tmpDatas);
                unlink($tmpZip);
                file::deleteFolder(DIR_TEMP . $folder);
                $zipInfo = pathinfo($_zip);
                alert::recSuccess("Restauration du backUp OK :. " . $zipInfo["filename"] . ".zip");
                return TRUE;
            } 
        }
        alert::recError("Restauration du backUp :. Erreur lors du backup");
        return FALSE;
    }

    public static function dumpMysql(string $_dir)
    {
        $backupFile = DB_NAME . ".Mysql";
        $command = "mysqldump --opt -h " . DB_HOST . " -u " . DB_USER . " -p" . DB_PASS . " " . DB_NAME . " > " . $_dir . "/" . $backupFile;
        try {
            system($command);
            return $backupFile;
        } catch (Exception $ex) {
            return FALSE;
        }
    }

    public static function dumpMysqlRestore(string $_backupFile)
    {
        $command = "mysql -h " . DB_HOST . " -u " . DB_USER . " -p" . DB_PASS . " " . DB_NAME . " < " . $_backupFile;
        try {
            system($command);
            return TRUE;
        } catch (Exception $ex) {
            return FALSE;
        }
    }

    public static function scanFolder()
    {
        $tempArray = array();
        if ($scandir = scandir(DIR_BACKUP)) {
            foreach ($scandir as $file) {
                $tempFile = NULL;
                if (is_file(DIR_BACKUP . $file) === TRUE and $file != "index.html") {
                    $tempFile["file"] = $file;
                    $tempFile["size"] = core::formatFileSize(filesize(DIR_BACKUP . $file), 2);
                    $tempFile["date"] = core::dateFromTimestamp(filemtime(DIR_BACKUP . $file));
                    $tempFile["timestamp"] = filemtime(DIR_BACKUP . $file);
                    $tempArray[] = $tempFile;
                }
            }
        }
        return $tempArray;
    }

    public static function last()
    {
        $return = NULL;
        foreach (self::scanFolder() as $key => $value) {
            $return[$value["timestamp"]] = $value;
        }
        krsort($return);
        return array_values($return)[0]["file"];
    }

    public static function delete(string $_nameBackup)
    {
        if(file_exists("/" . DIR_BACKUP . "/" . $_nameBackup)){
            unlink("/" . DIR_BACKUP . "/" . $_nameBackup);
            alert::recSuccess("BackUp :. Suppression de " . $_nameBackup);
            return TRUE;
        } else {
            alert::recError("BackUp :. Erreur lors de la suppression de " . $_nameBackup);
            return FALSE;
        }
    }

}
