Update index.php

This commit is contained in:
2024-09-22 12:21:10 +02:00
committed by GitHub
parent eba0f59d9a
commit 5f346d99bf
+101 -33
View File
@@ -1,26 +1,90 @@
<?php <?php
session_start(); session_start();
$directory = __DIR__ . '/files';
if (!file_exists($directory)) { // Password protection
$password = 'geheim'; // Replace with your desired password
$is_authenticated = isset($_SESSION['authenticated']) && $_SESSION['authenticated'] === true;
// Logout mechanism
if (isset($_GET['logout'])) {
unset($_SESSION['authenticated']);
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
if (!$is_authenticated) {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
if ($_POST['password'] === $password) {
$_SESSION['authenticated'] = true;
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
} else {
$error = 'Incorrect password';
}
}
// Display password prompt
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Protected</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
</head>
<body class="d-flex align-items-center justify-content-center" style="min-height: 100vh;">
<div class="modal d-block" tabindex="-1" style="background-color: rgba(0,0,0,0.5);">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Password Required</h5>
</div>
<div class="modal-body">
<form method="post">
<div class="mb-3">
<label for="password" class="form-label">Enter Password:</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
<?php
exit;
} else {
// Rest of the original code
$directory = __DIR__ . '/files';
if (!file_exists($directory)) {
mkdir($directory, 0755, true); mkdir($directory, 0755, true);
} }
$directory = realpath($directory); $directory = realpath($directory);
if ($directory === false || !is_dir($directory)) { if ($directory === false || !is_dir($directory)) {
die("Invalid directory"); die("Invalid directory");
} }
$files = glob($directory . '/*.md'); $files = glob($directory . '/*.md');
usort($files, function($a, $b) { usort($files, function($a, $b) {
return filemtime($b) - filemtime($a); return filemtime($b) - filemtime($a);
}); });
$latestFiles = array_slice($files, 0, 5); $latestFiles = array_slice($files, 0, 5);
$olderFiles = array_slice($files, 5); $olderFiles = array_slice($files, 5);
$alert = ''; $alert = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['action']) && isset($_POST['csrf_token']) && $_POST['csrf_token'] === $_SESSION['csrf_token']) { if (isset($_POST['action']) && isset($_POST['csrf_token']) && $_POST['csrf_token'] === $_SESSION['csrf_token']) {
if ($_POST['action'] === 'create' || $_POST['action'] === 'edit') { if ($_POST['action'] === 'create' || $_POST['action'] === 'edit') {
$date = date('Ymd'); $date = date('Ymd');
@@ -56,19 +120,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
} }
header('Location: ' . $_SERVER['PHP_SELF']); header('Location: ' . $_SERVER['PHP_SELF']);
exit; exit;
} }
$editFile = isset($_GET['edit']) ? basename($_GET['edit']) : null; $editFile = isset($_GET['edit']) ? basename($_GET['edit']) : null;
if ($editFile !== null && strpos($editFile, '..') !== false) { if ($editFile !== null && strpos($editFile, '..') !== false) {
$editFile = null; $editFile = null;
} }
if (empty($_SESSION['csrf_token'])) { if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32)); $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
} }
// Display alert from session if it exists // Display alert from session if it exists
if (isset($_SESSION['alert'])) { if (isset($_SESSION['alert'])) {
$alertType = $_SESSION['alert']['type']; $alertType = $_SESSION['alert']['type'];
$alertMessage = $_SESSION['alert']['message']; $alertMessage = $_SESSION['alert']['message'];
$alert = "<div class='alert alert-{$alertType} alert-dismissible fade show' role='alert'> $alert = "<div class='alert alert-{$alertType} alert-dismissible fade show' role='alert'>
@@ -76,9 +140,9 @@ if (isset($_SESSION['alert'])) {
<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button> <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
</div>"; </div>";
unset($_SESSION['alert']); unset($_SESSION['alert']);
} }
function renderFileListItem($file) { function renderFileListItem($file) {
$basename = basename($file); $basename = basename($file);
return " return "
<li class='list-group-item d-flex justify-content-between align-items-center'> <li class='list-group-item d-flex justify-content-between align-items-center'>
@@ -104,12 +168,12 @@ function renderFileListItem($file) {
</button> </button>
</div> </div>
</li>"; </li>";
} }
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vincent's MarkDown</title> <title>Vincent's MarkDown</title>
@@ -118,13 +182,14 @@ function renderFileListItem($file) {
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css"> <link rel="stylesheet" href="styles.css">
</head> </head>
<body class="d-flex flex-column min-vh-100"> <body class="d-flex flex-column min-vh-100">
<header class="py-2 sticky-header"> <header class="py-2 sticky-header">
<div class="container"> <div class="container d-flex justify-content-between align-items-center">
<a href="./" class="nav-brand"> <a href="./" class="nav-brand">
<strong>VINCENT</strong><span>MARKDOWN</span> <strong>VINCENT</strong><span>MARKDOWN</span>
</a> </a>
<a href="?logout" class="btn btn-outline-dark btn-sm">Logout</a>
</div> </div>
</header> </header>
@@ -271,5 +336,8 @@ function renderFileListItem($file) {
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/2.0.3/marked.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/2.0.3/marked.min.js"></script>
<script src="https://unpkg.com/easymde/dist/easymde.min.js"></script> <script src="https://unpkg.com/easymde/dist/easymde.min.js"></script>
<script src="scripts.js"></script> <script src="scripts.js"></script>
</body> </body>
</html> </html>
<?php
}
?>