<?php
function is_connected()
{
    $connected = @fsockopen("www.trustotech.com", 80);
    //website, port  (try 80 or 443)
    if ($connected) {
        $is_conn = true; //action when connected
        fclose($connected);
    } else {
        $is_conn = false; //action in connection failure
    }
    return $is_conn;
}
function is_windows_operating_system()
{
    return strtoupper(substr(PHP_OS/* php_uname('s') */, 0, 3)) === 'WIN';
}


//Path Default
//require_once __DIR__ . "/Includes/constants.php"; //we stop it and use above to fix for symbolic link
require_once  "common.php";
if (is_connected()) {

    $dir_zip = is_windows_operating_system() ? (__DIR__ . "/7z.exe") : "zip";
    //Connection String
    $host = DB_HOST;
    $user = DB_USER;
    $pass = DB_PASS;
    $dbname = DB_NAME;
    //
    $dir_export = $mysql_dump = '';
    $db = new db_standalone(null, $host, $user, $pass, $dbname);
    $stmt = $db->query_execute("SELECT setting_name, setting_value FROM tbl_default_settings
                WHERE setting_name in ('backup_db_export_path', 'backup_mysqldump_path') ");
    while ($stng_row = $db->query_fetch($stmt)) {
        if ($stng_row['setting_name'] == 'backup_db_export_path')
            $dir_export = $stng_row['setting_value'];
        elseif ($stng_row['setting_name'] == 'backup_mysqldump_path')
            $mysql_dump = $stng_row['setting_value'];
    }
    $org_det = $db->query_execute("SELECT * FROM tbl_organization
    WHERE Organization_ID = 1 ");
    $org_det_row = $db->query_fetch($org_det);
    $org_name = $org_det_row['Organization_Name'];
    //
    //
    // $dir_export = 'C:\xampp\htdocs\online_trusto\sync_backup\backup_files';
    $dir_export = __DIR__ . '\backup_files';
    if (empty($mysql_dump))
        $mysql_dump = get_mysqldump_path();
    $export = $dir_export . DIRECTORY_SEPARATOR . $dbname;
    $time = date('Y-m-d@H-i-s');
    $db_file = "{$export}@{$org_name}@{$time}.sql";
    $zip_file = "{$export}@{$org_name}@{$time}.zip";
    $fpt_file_name = "{$org_name}@{$time}.zip";
    //if host is with port ex: localhost:3306, then we must put the port in mysqldump path
    $tmp_db_host = $host;
    $tmp_db_port = '';
    $tmp_port_sep_pos = strpos($host, ':');
    if ($tmp_port_sep_pos !== false) {
        $tmp_db_host = substr($host, 0, $tmp_port_sep_pos);
        $tmp_db_port = substr($host, $tmp_port_sep_pos + 1);
    }


    //execution command to specific path and  put as zip file with password | we can echo exec or return result but add at last command " 2>&1"
    exec("\"{$mysql_dump}\" {$dbname} --routines --result-file=\"{$db_file}\" --host=\"{$tmp_db_host}\" " . (empty($tmp_db_port) ? "" : " --port=\"{$tmp_db_port}\" ") . " --user=\"{$user}\" " . (empty($pass) ? "" : " -p\"{$pass}\" ") . " ");
    if (is_windows_operating_system())
        exec("\"{$dir_zip}\" " .   ""   . " a \"{$zip_file}\" \"{$db_file}\" ");
    else
        exec("{$dir_zip} " .  ""   . " -j -r \"{$zip_file}\" \"{$db_file}\" ");
    // exec((is_windows_operating_system() ? "del" : "rm") . " \"{$db_file}\"");
    server_ftp_put_file($zip_file, '/', $fpt_file_name);
    server_ftp_close();


    /**
     * Functions
     */

    //
    $msg = true;
} else {
    $msg = false;
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Sync Backup</title>
    <link rel="stylesheet" href="./style.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>

</head>

<body>
    <div id="container">
        <?php if ($msg) { ?>
            <div id="success-box">
                <div class="dot"></div>
                <div class="dot two"></div>
                <div class="face">
                    <div class="eye"></div>
                    <div class="eye right"></div>
                    <div class="mouth happy"></div>
                </div>
                <div class="shadow scale"></div>
                <div class="message">
                    <h1 class="alert">Success!</h1>
                    <p>, everything is working.</p>
                </div>
                <button onclick="succ()" class="button-box">
                    <h1 class="green">continue</h1>
                </button>
            </div>
        <?php } else { ?>

            <div id="error-box">
                <div class="dot"></div>
                <div class="dot two"></div>
                <div class="face2">
                    <div class="eye"></div>
                    <div class="eye right"></div>
                    <div class="mouth sad"></div>
                </div>
                <div class="shadow move"></div>
                <div class="message">
                    <h1 class="alert">Error!</h1>
                    <p>oh no, computer is not connected.
                </div>
                <button onclick="error()" id="error_btn" class="button-box">
                    <h1 class="red">try again</h1>
                </button>
            </div>
    </div>
<?php } ?>

<script type="text/javascript">
    function error(){
        location.reload();
    }
    function succ(){
        location.href = '<?php echo SITE_LINK; ?>';
    }
</script>
</body>

</html>