<?php


ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL);
@$resetcode = $_POST['resetcode'];
@$backup = $_REQUEST['backup'];
@$UserID = $_REQUEST['user'];
@$Pass = $_REQUEST['pass'];
$bupdb = new SQLite3('./db/CloudBackup.db');
$bupdb->exec('CREATE TABLE IF NOT EXISTS BACKUP(id INTEGER PRIMARY KEY ,user TEXT, pass TEXT, resetcode TEXT, backup TEXT)');
$backedup = '{' . "\r\n\t" . '"tag":"CloudBackup",' . "\r\n\t" . '"success":"1",' . "\r\n\t" . '"api_ver":"1.4v",' . "\r\n\t" . '"version":"1.4",' . "\r\n\t" . '"msg":"Your backup has been completed successfully"' . "\r\n" . '}';
$badpass = '{' . "\r\n\t" . '"tag":"CloudBackup",' . "\r\n\t" . '"success":"0",' . "\r\n\t" . '"api_ver":"1.4v",' . "\r\n\t" . '"version":"1.4",' . "\r\n\t" . '"msg":"Backup Password is incorrect. Please check your password and try again. Code: BPE"' . "\r\n" . '}';
$nouser = '{"tag":"CloudBackup","success":"0","api_ver":"1.0v","version":"1.0","msg":"Your Cloud Backup has been failed. Please try again or contact support. Code: JE"}';
$bupres = $bupdb->query('SELECT * FROM BACKUP WHERE user=\'' . $UserID . '\'');

while ($buprow = $bupres->fetchArray()) {
	$dbuser = $buprow['user'];
	$dbpass = $buprow['pass'];
	$dbresetcode = $buprow['resetcode'];
	$dbbackup = $buprow['backup'];
}

if (isset($resetcode)) {
	if ($UserID !== @$dbuser) {
		$bupdb->exec('INSERT INTO BACKUP (user, pass, resetcode, backup) VALUES(\'' . $UserID . '\', \'' . $Pass . '\', \'' . $resetcode . '\', \'' . $backup . '\') ');
		echo $backedup;
	}
	else {
		if ($UserID == @$dbuser) {
			$bupdb->exec('UPDATE BACKUP SET backup=\'' . $backup . '\' WHERE user=\'' . $UserID . '\'');
			echo $backedup;
		}
		else {
			echo $nouser;
		}
	}
}

if ($resetcode == '') {
	if ($Pass == @$dbpass) {
		echo "\r\n" . '{' . "\r\n" . '"tag":"CloudBackup",' . "\r\n" . '"success":"1",' . "\r\n" . '"api_ver":"1.4v",' . "\r\n\t" . '"version":"1.4",' . "\r\n" . '"backup":"' . @$dbbackup . '",' . "\r\n" . '"msg":"Backup has been downloaded successfully"' . "\r\n" . '}';
	}
	else {
		echo $badpass;
	}
}
else {
	echo $nouser;
}

?>