<!DOCTYPE html>
<html>
	<head>
		<title>
			<?php
				if (isset($_GET['action']) && $_GET['action'] == 'open')
				{
					echo $_GET['param'];
				}
			?>
		</title>
		<link rel="manifest" href="manifest.json" />
		<meta name="theme-color" content="white" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="style.css">
	</head>
	<body>
		<?php
			require 'settings.php';

			if ($password && (!isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_PW'] != $password)) {
				header('WWW-Authenticate: Basic realm="bbn"');
				header('HTTP/1.0 401 Unauthorized');
				echo '<p>Access denied.</p>';
				exit;
			}

			$action = '';

			if (isset($_GET['open']))
			{
			  $action = 'open';
			}
			else if (isset($_GET['filter']))
			{
			  $action = 'filter';
			}
			else if (isset($_GET['search']))
			{
			$action = 'search';
			}
			else if (isset($_GET['tags']))
			{
			$action = 'tags';
			}
			else if (isset($_GET['clip']))
			{
			$action = 'clip';
			}
			else if (isset($_POST['save']))
			{
			$action = 'save';
			}
			else if (isset($_POST['delete']))
			{
			$action = 'delete';
			}
			else if (isset($_POST['home']))
			{
				$action = 'save';
			}
			else if(isset($_GET['home']))
			{
				$action = '';
			}
			else if (isset($_POST['preview']))
			{
			  $action = 'preview';
			}

			$content = '';
			if ($action == 'clip' && $_GET['param'])
			{
			 	$content = $_GET['param'] . "\r\n" . file_get_contents($dir . '/clip');
				file_put_contents($dir . '/clip', $content);
				$_GET['param'] = '';
			}
			if ($action == 'save')
			{
				$title = $_POST['title'];
				$content = $_POST['content'];
				file_put_contents($dir . '/' . $title, $content);

				$previoustitle = $_POST['previoustitle'];
				if ($title != $previoustitle)
				{
					rename($dir . '/' . $previoustitle, $dir . '/' . $previoustitle . '.del');
				}

				$action = 'open';
				$_GET['param'] = $title;

				if (isset($_POST['home']))
				{
					header('Location: index.php');
				}
			}
			else if ($action == 'delete')
			{
				$title = $_POST['title'];
				rename($dir . '/' . $title, $dir . '/' . $title . '.del');
			}

			if ($action == 'preview')
			{
				require 'libs/Parsedown.php';
				$title = $_POST['title'];
				$content = $_POST['content'];
				$pos = 0;
				if (str_starts_with($content, '---'))
				{
					$pos = strpos($content, '---', 3);
				}
				$Parsedown = new Parsedown();
				$Parsedown->setBreaksEnabled(true);
				echo $Parsedown->text('# ' . $title . "\r\n" . substr($content, $pos + 3));
			}
			else if ($action == 'open')
			{
			  $title = $_GET['param'];
				$now = date("Y-m-d H.i.s", time());
				if (!$title)
				{
					$title = $now;
				}

				if (!file_exists($dir . '/' . $title))
				{
					$content = "---\r\ndate: " . substr($now, 0, 10) . "\r\ntags: \r\n---\r\n";
					file_put_contents($dir . '/' . $title, $content);
				}
				else if (!$content)
				{
					$content = file_get_contents($dir . '/' . $title);
				}
				require('note.php');
			}
			else
			{
				if (isset($_GET['param']))
				{
					$param = $_GET['param'];
				}
				require('home.php');
			}
		?>
	</body>
</html>