67 lines
1.9 KiB
PHP
Executable file
67 lines
1.9 KiB
PHP
Executable file
<?php
|
|
// config
|
|
define("DATA_FILE", "pastes.json");
|
|
define("BASE_URL", "http://localhost/index.php");
|
|
|
|
$pastes = file_exists(DATA_FILE) ? json_decode(file_get_contents(DATA_FILE), true) : [];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (!empty($_POST['content'])) {
|
|
$id = uniqid();
|
|
$pastes[$id] = [
|
|
'content' => $_POST['content'],
|
|
'timestamp' => time()
|
|
];
|
|
file_put_contents(DATA_FILE, json_encode($pastes));
|
|
header("Location: " . BASE_URL . "?id=$id");
|
|
exit;
|
|
}
|
|
} elseif (isset($_GET['delete']) && isset($_GET['id'])) {
|
|
$id = $_GET['id'];
|
|
if (isset($pastes[$id])) {
|
|
unset($pastes[$id]);
|
|
file_put_contents(DATA_FILE, json_encode($pastes));
|
|
header("Location: " . BASE_URL);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
if (isset($_GET['id'])) {
|
|
$id = $_GET['id'];
|
|
if (isset($pastes[$id])) {
|
|
echo "<h1>paste $id</h1>";
|
|
echo "<pre>" . htmlspecialchars($pastes[$id]['content']) . "</pre>";
|
|
echo "<a href='" . BASE_URL . "'>Create new paste</a> | ";
|
|
echo "<a href='" . BASE_URL . "?delete=true&id=$id'>Delete this paste</a>";
|
|
exit;
|
|
} else {
|
|
echo "Paste not found.";
|
|
exit;
|
|
}
|
|
}
|
|
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>pastry</title>
|
|
</head>
|
|
<body>
|
|
<h1>new paste</h1>
|
|
<form method="post">
|
|
<textarea name="content" rows="10" cols="50" placeholder="enter text" required></textarea><br>
|
|
<button type="submit">create</button>
|
|
</form>
|
|
|
|
<h2>recent pastes</h2>
|
|
<ul>
|
|
<?php foreach (array_slice($pastes, -5, 5, true) as $id => $paste): ?>
|
|
<li>
|
|
<a href="php echo BASE_URL . '?id-' . $id; ?>">view paste <?php echo $id; ?></a>
|
|
(Created: <?php echo date('Y-m-d H:i:s', $paste['timestamp']); ?>)
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</body>
|
|
</html>
|