<?php

include_once 'functions.php';
$username = $_POST['username'];
$password = $_POST['password'];
//check for #user file. no userfile = redirect to setup
if (filesize("#user.php") < 1) {
    header("Location: ./setup.php");
    die;
}
//authorise and set session and redirect
if ($username && $password) {
    include "#user.php";
    //decode password
    $rightusername = decodeuserdata($rightusername);
    $rightpassword = decodeuserdata($rightpassword);
    if ($username == $rightusername && $password == $rightpassword) {
        session_start();
        $_SESSION['auth'] = 1;
        header("Location: ./upload.php");
        die;
    }
}
//check session and redirect
session_start();
if ($_SESSION['auth'] == 1) {
    header("Location: ./upload.php");
    die;
}
?>
<html>
if ($_POST['Button_save'] == "Save") {
    $save = new SaveConfig();
    $save->srightusername = $_POST['newusername'];
    $save->srightpassword = $_POST['newpassword'];
    $save->srightpassword2 = $_POST['newpassword2'];
    $save->sexternarchivepath = $_POST['externarchivepath'];
    $save->sarchive_path = $_POST['archivepath'];
    $save->supload_dir = $_POST['uploadpath'];
    $save->sdefault_mp3_part_length = $_POST['defaultlength'];
    $save->save();
}
include "./config.php";
//fill in existing Userdata
if (filesize("./#user.php") > 1) {
    include "./#user.php";
    $rightusername = decodeuserdata($rightusername);
    $externarchivepath = $extern_archive_path;
    $archive_path = $archive_path;
    $upload_dir = $upload_dir;
    $default_mp3_part_length = $default_mp3_part_length;
} else {
    $currentpath = 'http://' . $_SERVER['HTTP_HOST'] . str_replace("setup.php", "", $_SERVER['SCRIPT_NAME']);
    $externarchivepath = $currentpath . "archive";
    $archive_path = "archive";
    $upload_dir = "upload";
    $default_mp3_part_length = "10";
}
class SaveConfig
{
    public $srightusername;
    public $srightpassword;