<?php

// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");
//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');

//Programsetting
$Programsetting = new Programsetting();
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$ProgramsettingEX = new ProgramsettingsMySqlExtDAO();



$db_user = ConnectionProperty::getUser();
$host = ConnectionProperty::getHost();
$db_password = ConnectionProperty::getPassword();
$generalSettings = $ProgramsettingDAO->load(1);
//getcwd() is to Gets the current working directory
$partition = explode(":", getcwd())[0]; //$generalSettings->partition;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    //do nothing
} else {//if (!empty(exec("ip link"))) {// unix, linux, mac
    $partition = 0;
}
/* if (DIRECTORY_SEPARATOR === '/') {
  // unix, linux, mac
  }
  if (DIRECTORY_SEPARATOR === '\\') {
  // windows
  } */

if ($partition !== $generalSettings->partition) {
    $generalSettings->partition = $partition;
    $ProgramsettingDAO->update($generalSettings);
} else {
    $partition = $generalSettings->partition;
}


$dbname = $_SESSION['dbname'];

//$newname = date("Y-m-d");
//$inrtoi = "..\db_backups\dbbackup_$newname.sql";
//$command = "$partition:\\wamp\bin\mysql\mysql5.6.17\bin\mysqldump --user=$db_user --password=$db_password --host=$host --opt $dbname > $inrtoi";
##1-get $dbDumpResult
//$command = "$partition:\\wamp\bin\mysql\mysql5.6.17\bin\grep -v CREATE VIEW "; //dont use any command of CREATE VIEW as it make problem when importing backups
if (empty($partition) || $partition == "0") {//linux
    ##/usr/bin/mysqldump
    $command = "mysqldump --user=$db_user --password=$db_password --host=$host --opt $dbname"; //--compact --ignore-table=$dbname.sellbillview
    exec($command, $output, $result);
    $output = implode(PHP_EOL, $output);
} else {//localhost
    //mysqldump -u dbUser -p DBName | gzip > OutputFile.sql.gz
    $command = "$partition:\\wamp\bin\mysql\mysql5.6.17\bin\mysqldump --user=$db_user --password=$db_password --host=$host --opt $dbname"; //--compact --ignore-table=$dbname.sellbillview
    //$output = passthru($command . " 2>&1");
    $output = passthru($command); //remove " 2>&1" as it writes warning to backup file
}

$file = fopen('../db_backups/temp__manualBackup/' . $_SESSION['dbname'] . '_' . date("Y-m-d") . '.sql', "w"); //date("Y-m-d H:i:s")
fwrite($file, $output);
fclose($file);
##2-make download and user save it any where
//https://stackoverflow.com/questions/3697748/fastest-way-to-serve-a-file-using-php
$project_name = explode('/controllers', $_SERVER["PHP_SELF"])[0];
$file_name = $_SERVER["DOCUMENT_ROOT"] . "$project_name/db_backups/temp__manualBackup/" . $_SESSION['dbname'] . "_" . date("Y-m-d") . ".sql";

//header("X-Sendfile: $file_name");//not working in cpanel use echo
echo $output;
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($file_name) . '"');
##del file
unlink($file_name);
?>