<?php
defined('VSIGN') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");

class Maintenance extends Data
{
    /**
     * Backup the Database
     * @param <string> $saveAs file|string
     */
    public function BackupDatabase($saveAs = "string")
    {
        // Always truncate the log first
        $this->db->query("TRUNCATE TABLE `log` ");
        $this->db->query("TRUNCATE TABLE `oauth_log` ");

        global $dbhost;
        global $dbuser;
        global $dbpass;
        global $dbname;

        // Run mysqldump to a temporary file

        // get temporary file
        $tempFile = tempnam(Config::GetSetting($this->db, 'LIBRARY_LOCATION'), 'dmp');

        exec('mysqldump --opt --host=' . $dbhost . ' --user=' . $dbuser . ' --password=' . $dbpass . ' ' . $dbname . ' > ' . escapeshellarg($tempFile) . ' ');

        $sqlDump = file_get_contents($tempFile);

        unlink($tempFile);

        return $sqlDump;
    }

    /**
     * Restore Database
     * @param <string> $fileName
     */
    public function RestoreDatabase($fileName)
    {
        global $dbhost;
        global $dbuser;
        global $dbpass;
        global $dbname;
        
        // Push the file into msqldump
        exec('mysql --user=' . $dbuser . ' --password=' . $dbpass . ' ' . $dbname . ' < ' . escapeshellarg($fileName) . ' ');

        Debug::LogEntry($this->db, 'audit', 'mysql --user=' . $dbuser . ' --password=' . $dbpass . ' ' . $dbname . ' < ' . escapeshellarg($fileName) . ' ' );

        return true;
    }
}
?>
