mirror of
https://git.sr.ht/~iwakuralain/text0Nly
synced 2025-07-27 15:36:11 +00:00
134 lines
4.7 KiB
PHP
134 lines
4.7 KiB
PHP
<?php
|
|
header('X-Content-Type-Options: nosniff');
|
|
header('X-Frame-Options: DENY');
|
|
header('X-XSS-Protection: 1; mode=block');
|
|
header('Content-Security-Policy: default-src \'self\'');
|
|
|
|
$config = require 'config.php';
|
|
|
|
try {
|
|
$db = new PDO(
|
|
"mysql:host={$config['db']['host']};dbname={$config['db']['name']}",
|
|
$config['db']['user'],
|
|
$config['db']['pass']
|
|
);
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (PDOException $e) {
|
|
die('Connection failed: ' . $e->getMessage());
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
|
|
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
|
|
$signature = $_POST['signature'] ?? '';
|
|
$is_encrypted = isset($_POST['encrypted']) ? 1 : 0;
|
|
|
|
if ($username && $message) {
|
|
try {
|
|
$stmt = $db->prepare('INSERT INTO messages (username, message, signature, is_encrypted) VALUES (?, ?, ?, ?)');
|
|
$stmt->execute([$username, $message, $signature, $is_encrypted]);
|
|
} catch (PDOException $e) {
|
|
die('Database error: ' . $e->getMessage());
|
|
}
|
|
}
|
|
header('Location: ' . $_SERVER['PHP_SELF']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$stmt = $db->query('SELECT * FROM messages ORDER BY created_at DESC LIMIT 50');
|
|
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
die('Database error: ' . $e->getMessage());
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Text0Nly</title>
|
|
<link rel="stylesheet" href="styles.css">
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="header">
|
|
<h1>Text0Nly</h1>
|
|
<div class="header-buttons">
|
|
<a href="#" onclick="showApiInfo()">API Info</a>
|
|
<a href="register.php">Register</a>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="messages-container" id="messages">
|
|
<?php foreach ($messages as $msg): ?>
|
|
<div class="message">
|
|
<span class="username"><?= htmlspecialchars($msg['username']) ?></span>
|
|
<?php if ($msg['is_encrypted']): ?>
|
|
<span class="encrypted">[Encrypted]</span>
|
|
<?php endif; ?>
|
|
<span class="time"><?= $msg['created_at'] ?></span>
|
|
<div class="clear"></div>
|
|
<div class="message-content"><?= nl2br(htmlspecialchars($msg['message'])) ?></div>
|
|
<?php if ($msg['signature']): ?>
|
|
<div class="signature">Signature: <?= htmlspecialchars($msg['signature']) ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<div class="form-container">
|
|
<form method="post">
|
|
<input type="text" name="username" placeholder="Your username" required maxlength="50">
|
|
<textarea name="message" placeholder="Your message" required></textarea>
|
|
<textarea name="signature" placeholder="PGP signature (optional)"></textarea>
|
|
<div class="checkbox-wrapper">
|
|
<input type="checkbox" name="encrypted" id="encrypted">
|
|
<label for="encrypted">Message is encrypted</label>
|
|
</div>
|
|
<button type="submit">Send Message</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="apiModal" class="modal">
|
|
<div class="modal-content">
|
|
<span class="close" onclick="hideApiInfo()">×</span>
|
|
<h2>Message API</h2>
|
|
<div class="api-info">
|
|
<pre>curl -X POST http://localhost/api.php -H "Content-Type: application/json" -d '{
|
|
"username": "name",
|
|
"message": "text",
|
|
"signature": "pgp_signature",
|
|
"encrypted": true
|
|
}'</pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function showApiInfo() {
|
|
document.getElementById('apiModal').style.display = 'block';
|
|
}
|
|
|
|
function hideApiInfo() {
|
|
document.getElementById('apiModal').style.display = 'none';
|
|
}
|
|
|
|
window.onclick = function(event) {
|
|
if (event.target == document.getElementById('apiModal')) {
|
|
hideApiInfo();
|
|
}
|
|
}
|
|
|
|
setInterval(() => {
|
|
fetch(window.location.href)
|
|
.then(response => response.text())
|
|
.then(html => {
|
|
const parser = new DOMParser();
|
|
const doc = parser.parseFromString(html, 'text/html');
|
|
document.getElementById('messages').innerHTML = doc.getElementById('messages').innerHTML;
|
|
});
|
|
}, 5000);
|
|
</script>
|
|
</body>
|
|
</html>
|