mirror of
https://git.sr.ht/~iwakuralain/text0Nly
synced 2025-07-27 07:30:31 +00:00
114 lines
4.2 KiB
PHP
114 lines
4.2 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\'');
|
|
|
|
try {
|
|
$db = new PDO('mysql:host=localhost;dbname=messenger', 'root', '');
|
|
$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']);
|
|
|
|
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>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
|
|
.message { margin: 10px 0; padding: 10px; border: 1px solid #ddd; border-radius: 5px; }
|
|
.username { font-weight: bold; color: #2196F3; }
|
|
.time { color: #666; font-size: 0.8em; }
|
|
.encrypted { color: #4CAF50; }
|
|
.signature { color: #FF9800; font-size: 0.8em; }
|
|
form { margin: 20px 0; }
|
|
textarea { width: 100%; height: 100px; margin: 10px 0; }
|
|
input[type="text"] { width: 200px; }
|
|
.register-link { position: fixed; top: 20px; right: 20px; }
|
|
.api-info { margin: 20px 0; padding: 10px; background: #f5f5f5; border-radius: 5px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="register-link">
|
|
<a href="register.php">Register</a>
|
|
</div>
|
|
|
|
<h1>Text0Nly</h1>
|
|
|
|
<div class="api-info">
|
|
<h3>Message API:</h3>
|
|
<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>
|
|
|
|
<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>
|
|
<input type="checkbox" name="encrypted" id="encrypted">
|
|
<label for="encrypted">Message is encrypted</label>
|
|
</div>
|
|
<button type="submit">Send</button>
|
|
</form>
|
|
|
|
<div id="messages">
|
|
<?php foreach ($messages as $msg): ?>
|
|
<div class="message">
|
|
<span class="username"><?= htmlspecialchars($msg['username']) ?>:</span>
|
|
<div><?= nl2br(htmlspecialchars($msg['message'])) ?></div>
|
|
<?php if ($msg['is_encrypted']): ?>
|
|
<span class="encrypted">[Encrypted]</span>
|
|
<?php endif; ?>
|
|
<?php if ($msg['signature']): ?>
|
|
<div class="signature">Signature: <?= htmlspecialchars($msg['signature']) ?></div>
|
|
<?php endif; ?>
|
|
<span class="time"><?= $msg['created_at'] ?></span>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<script>
|
|
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>
|