<?php

error_reporting(0);
ini_set('default_charset', 'UTF-8');



if (isset($_GET['debug'])) {
	print("SA:   ");
	$debug = urlencode(file_get_contents(dirname(__FILE__) . "/../template/Austritt_Beistand.txt"));
	die($debug);
}

if (isset($_GET['template'])) {
	$current_dir = dirname(__FILE__);
	if (is_file($current_dir.'/../template/' . $_GET['template'] . '.txt')) {
		$pathinfo = pathinfo($current_dir.'/../template/' . $_GET['template'] . '.txt');
		if ($pathinfo['extension'] == 'txt') {
			$str = file_get_contents($current_dir.'/../template/' . $_GET['template'] . '.txt');

			if (is_file($current_dir.'/../template/' . $_GET['template'] . '-form.txt')) {
				$pathinfoForm = pathinfo($current_dir.'/../template/' . $_GET['template'] . '-form.txt');
				if ($pathinfoForm['extension'] == 'txt') {
					$str = $str."!JSON_placeholder:".file_get_contents($current_dir.'/../template/' . $_GET['template'] . '-form.txt');
				}
			}

			print(rawurlencode($str));
			die();
		}
	} else {
		print_r('error: File not found');
		die();
	}
}

if (isset($_GET['templates'])) {
	$current_dir = dirname(__FILE__);
	$directory = $current_dir . '/../template';
	$arr = array_diff(scandir($directory), array('..', '.'));
	$arr_res = [];
	foreach($arr as $x) {
		$pathinfo = pathinfo($current_dir . '/../template/' . $x);

		$file = $pathinfo['filename'];

		//skip textBlocksFile
		if ($file == "_textBlocks") {
			continue;
		}

		$file = str_replace("_", " ", $file);
		if (substr($file, -5, 5) != "-form") {
			$arr_res[] = [$file, $pathinfo['filename']];
		}
	}
	print(json_encode($arr_res));

}


if (isset($_GET['textBlocks'])) {

	
	$current_dir = dirname(__FILE__);
	if (is_file($current_dir.'/../template/_textBlocks.txt')) {
			$str = file_get_contents($current_dir.'/../template/_textBlocks.txt');
			$arr = preg_split('/\r\n|\r|\n/', $str);
			$arr_res = [];
			foreach($arr as $x) {
				$id = substr($x, 0, strpos($x, ":"));
				$txt = substr($x, strpos($x, ":")+2);
				$arr_res[] = array($id, $txt);
			}
			print(json_encode($arr_res));
			die();
	} else {
		print_r('error: File not found');
		die();
	}
}

if (isset($_GET['setForm'])) {
	$data = json_decode(file_get_contents('php://input'), true);
	$formFile = dirname(__FILE__) . '/../template/' . $data['template'] . '-form.txt';

	if (is_file($formFile)) {
		unlink($formFile);
	}

	file_put_contents($formFile, $data['data']);

	die("Creation successful");
}

if (isset($_GET['setTemplate'])) {
	$data = json_decode(file_get_contents('php://input'), true);
	$formFile = dirname(__FILE__) . '/../template/' . $data['fileName'] . '.txt';

	if (is_file($formFile)) {
		unlink($formFile);
	}

	file_put_contents($formFile, $data['data']);

	die("Creation successful");
}

if (isset($_GET['storeFiles'])) {
	$data = json_decode(file_get_contents('php://input'), true);
	$formFile = dirname(__FILE__) . '/../storage/' . $data['0']['data'] . '.txt';

	if (is_file($formFile)) {
		unlink($formFile);
	}

	file_put_contents($formFile, json_encode($data));
	die("success");
}

if (isset($_GET['storedFiles'])) {
	$data = json_decode(file_get_contents('../storage/' . $_GET['storedFiles'] . '.txt'), true);
	$ts = $data[0]['ts'];

	if (isset($_GET['del'])) {
		unlink('../storage/' . $_GET['storedFiles'] . '.txt');
		$ts = "";
	}

	if ($ts != "") {
		die($ts);
	} else {
		die("none");
	}
}

if (isset($_GET['getStoredFiles'])) {
	$data = json_decode(file_get_contents('../storage/' . $_GET['getStoredFiles'] . '.txt'), true);
	$ts = $data[0]['ts'];


	if ($ts != "") {
		die(rawurlencode(json_encode($data)));
	} else {
		die("none");
	}
}


if (count($_GET) <= 0) {
	header("Location: /");
	die();
}


if(isset($_FILES['file'])){
	die('Upload failed!');
	$expensions= array("txt", "md");
    $errors = '';
	$file_name = $_FILES['file']['name'];
	$file_size =$_FILES['file']['size'];
	$file_tmp =$_FILES['file']['tmp_name'];
	$file_type=$_FILES['file']['type'];
	$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));

	if(!in_array($file_ext,$expensions)){
		$errors = "3";
	}
	//if($file_size > 200000000000000000000000) {
    //       $errors = "4";
	//}
	if(empty($errors)){
			move_uploaded_file($file_tmp, dirname(__FILE__)."/../template/" . $file_name);
	} else {
		header($_SERVER["SERVER_PROTOCOL"]." 406 Not Acceptable");
		die("Upload failed!");
	}
	header($_SERVER["SERVER_PROTOCOL"]." 201 Created");
	print("Upload was successful!");
}

?>
