diff --git a/main/db.sql b/main/db.sql index fff923f..65bcf83 100644 --- a/main/db.sql +++ b/main/db.sql @@ -12,7 +12,8 @@ CREATE TABLE users ( username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, pgp_key TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + is_moderator TINYINT(1) NOT NULL DEFAULT 0 ); CREATE TABLE registrations ( diff --git a/main/index.php b/main/index.php index 214d13d..12f0cb5 100644 --- a/main/index.php +++ b/main/index.php @@ -1,4 +1,5 @@ getMessage()); } +if (isset($_GET['logout'])) { + session_destroy(); + header('Location: index.php'); + exit; +} + +function is_registered($db, $username) { + $stmt = $db->prepare('SELECT id FROM users WHERE username = ?'); + $stmt->execute([$username]); + return $stmt->fetchColumn() ? true : false; +} + 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()); + if (is_registered($db, $username)) { + if (!isset($_SESSION['username']) || $_SESSION['username'] !== $username) { + $error = 'Это имя занято. Войдите для отправки.'; + } + } + if (empty($error)) { + 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; } } - header('Location: ' . $_SERVER['PHP_SELF']); +} + +if (isset($_GET['delete']) && isset($_SESSION['is_moderator']) && $_SESSION['is_moderator']) { + $msg_id = (int)$_GET['delete']; + $db->prepare('DELETE FROM messages WHERE id = ?')->execute([$msg_id]); + header('Location: index.php'); exit; } @@ -54,10 +81,20 @@ try {

Text0Nly

- Register + + 👤 + Logout + + Login + Register +
+ +
+ +
@@ -71,13 +108,19 @@ try {
Signature:
+ +
+ + +
+
- +
diff --git a/main/login.php b/main/login.php new file mode 100644 index 0000000..68f271e --- /dev/null +++ b/main/login.php @@ -0,0 +1,57 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +$error = ''; +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING); + $password = $_POST['password'] ?? ''; + if ($username && $password) { + $stmt = $db->prepare('SELECT id, password, is_moderator FROM users WHERE username = ?'); + $stmt->execute([$username]); + $user = $stmt->fetch(PDO::FETCH_ASSOC); + if ($user && password_verify($password, $user['password'])) { + $_SESSION['user_id'] = $user['id']; + $_SESSION['username'] = $username; + $_SESSION['is_moderator'] = $user['is_moderator']; + header('Location: index.php'); + exit; + } else { + $error = 'Invalid username or password'; + } + } +} +?> + + + + + Login + + + +
+

Login

+ +
+ + + + + + +
Register | Back to chat
+
+ + \ No newline at end of file