<?php
session_start();
function generatekey($int) {
	$chars = '1234567890\'?qwertzuiopQWERTZUIOP!]asdfghjklASDFGHJKL${}<>\\yxcvbnmYXCVBNM;.:-_';
	$str = '';
	for ($i = 1; $i <= $int; $i++) {
		$str .= substr($chars,rand(1,strlen($chars) - 1),1);
	}
	return $str;
}
require('../functions.php');
unlock_all_files();
$config = loadConfig();
if(o_login_check($_SESSION['user'],$_SESSION['password'])) {
	if(!isset($_GET['step'])) {
		move_uploaded_file($_FILES['conf']['tmp_name'],'tmp_opb_conf.tmp.opbconf.txt');
		$file = file('tmp_opb_conf.tmp.opbconf.txt');
		foreach($file AS $setting) {
			$settings[substr($setting,0,strpos($setting,'='))] = trim(substr($setting,strpos($setting,'=') + 1));
		}
		echo '<!DOCTYPE html><link rel="Stylesheet" href="../style.php"><link rel="Shortcut Icon" href="../opolytics.png"><title>Laden der Konfiguration für Opolytics Backup</title></head><body>';
		include('../menu.php');
		echo '<h1>Laden der Konfiguration</h1>';
		if($settings['EXHIBITED_FOR_HOST']==$_SERVER['SERVER_NAME']) echo '<p>Der als Standardhost angegebene Host entspricht dem aktuellen Host.</p>';
		else if($settings['EXHIBITED_FOR_HOST']!=$_SERVER['SERVER_NAME']) echo '<p>Der als Standardhost angegebene Host entspricht <b>nicht</b> dem aktuellen Host.</p>';
		else if(in_array($_SERVER['SERVER_NAME'],array_map('trim',explode(',',$settings['EXHIBITED_FOR_ALTERNATIVE_HOSTS'])))) echo '<p>Der aktuelle Host ist als alternativer Host angegeben</p>';
		else die('<p>Der aktuelle Host ist auch nicht als alternativer Host angegeben, die Konfiguration wurde abgebrochen.</p>');
		echo '<h2>Produktinformationen</h2><p>Die folgende Konfiguration war in der Konfigurationsdatei enthalten.</p><table><tbdody><tr><td>Speicherdauer pro Backup</td><td>'.$settings['PACKAGE_PROVIDED_BACKUP_SAVING_DURATION'].' Tage</td></tr><tr><td>Laufzeit bis</td><td>'.date("d.m.Y",$settings['PACKAGE_PROVIDED_UNTIL']).'</td></tr></tbody></table><p><input type="button" onclick="window.location.href=\'?step=1\'" value="Bestätigen"></p>';
	} else {
		$file = file('tmp_opb_conf.tmp.opbconf.txt');
		foreach($file AS $setting) {
			$settings[substr($setting,0,strpos($setting,'='))] = trim(substr($setting,strpos($setting,'=') + 1));
		}
		$opbconf = array($settings['USER_COMMON_TRANSPORT_KEY'],generatekey(256),$settings['PACKAGE_PROVIDED_BACKUP_SAVING_DURATION'],$settings['PACKAGE_PROVIDED_UNTIL']);
		file_put_contents('opbconf.txt',implode(', ',$opbconf));
		unlink('tmp_opb_conf.tmp.opbconf.txt');
		echo '<!DOCTYPE html><link rel="Stylesheet" href="../style.php"><link rel="Shortcut Icon" href="../opolytics.png"><title>Laden der Konfiguration für Opolytics Backup</title></head><body>';
		include('../menu.php');
		echo '<h1>Laden der Konfiguration abgeschlossen</h1><p>Der Assistent hat Opolytics Backup soeben eingerichtet.</p><p><input type="button" onclick="window.location.href=\'../settings.php?show=updates\'" value="Abschliessen"></p>';
	}
}
?>
<style>
td:nth-child(odd) {
	font-weight: bold;
}
</style>