txt-notepad/index.php

67 lines
2.1 KiB
PHP

<?php
// Base URL of the website, without trailing slash.
$base_url = 'https://notes.orga.cat';
// Path to the directory to save the notes in, without trailing slash.
// Should be outside the document root, if possible.
$save_path = '_tmp';
// Disable caching.
header('Cache-Control: no-store');
// If no note name is provided, or if the name is too long, or if it contains invalid characters.
if (!isset($_GET['note']) || strlen($_GET['note']) > 64 || !preg_match('/^[a-zA-Z0-9_-]+$/', $_GET['note'])) {
// Generate a name with 5 random unambiguous characters. Redirect to it.
header("Location: $base_url/" . substr(str_shuffle('234579abcdefghjkmnpqrstwxyz'), -5));
die;
}
$path = $save_path . '/' . $_GET['note'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$text = isset($_POST['text']) ? $_POST['text'] : file_get_contents("php://input");
// Update file.
file_put_contents($path, $text);
// If provided input is empty, delete file.
if (!strlen($text)) {
unlink($path);
}
die;
}
// Print raw file when explicitly requested, or if the client is curl or wget.
if (isset($_GET['raw']) || strpos($_SERVER['HTTP_USER_AGENT'], 'curl') === 0 || strpos($_SERVER['HTTP_USER_AGENT'], 'Wget') === 0) {
if (is_file($path)) {
header('Content-type: text/plain');
readfile($path);
} else {
header('HTTP/1.0 404 Not Found');
}
die;
}
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php print $_GET['note']; ?></title>
<link rel="icon" href="<?php print $base_url; ?>/favicon.ico" sizes="any">
<link rel="icon" href="<?php print $base_url; ?>/favicon.svg" type="image/svg+xml">
<link rel="stylesheet" href="<?php print $base_url; ?>/styles.css">
</head>
<body>
<div class="container">
<textarea id="content"><?php
if (is_file($path)) {
print htmlspecialchars(file_get_contents($path), ENT_QUOTES, 'UTF-8');
}
?></textarea>
</div>
<pre id="printable"></pre>
<script src="<?php print $base_url; ?>/script.js"></script>
</body>
</html>