commit 7d1a18495246c4a05a5740a213e40bb56ea493de Author: Lain Iwakura Date: Mon Jun 16 00:12:10 2025 +0300 first commit diff --git a/configs/apache.conf b/configs/apache.conf new file mode 100644 index 0000000..db3f6d7 --- /dev/null +++ b/configs/apache.conf @@ -0,0 +1,13 @@ + + ServerName messenger.local + DocumentRoot /var/www/html/main + + + Options Indexes FollowSymLinks + AllowOverride All + Require all granted + + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + \ No newline at end of file diff --git a/main/api.php b/main/api.php new file mode 100644 index 0000000..d31f6bf --- /dev/null +++ b/main/api.php @@ -0,0 +1,39 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +if ($_SERVER['REQUEST_METHOD'] !== 'POST') { + http_response_code(405); + die(json_encode(['error' => 'Method not allowed'])); +} + +$input = json_decode(file_get_contents('php://input'), true); +if (!$input) { + http_response_code(400); + die(json_encode(['error' => 'Invalid JSON'])); +} + +$username = filter_var($input['username'] ?? '', FILTER_SANITIZE_STRING); +$message = $input['message'] ?? ''; +$signature = $input['signature'] ?? ''; +$is_encrypted = !empty($input['encrypted']); + +if (!$username || !$message) { + http_response_code(400); + die(json_encode(['error' => 'Missing required fields'])); +} + +try { + $stmt = $db->prepare('INSERT INTO messages (username, message, signature, is_encrypted) VALUES (?, ?, ?, ?)'); + $stmt->execute([$username, $message, $signature, $is_encrypted]); + echo json_encode(['success' => true]); +} catch (Exception $e) { + http_response_code(500); + echo json_encode(['error' => 'Server error']); +} \ No newline at end of file diff --git a/main/db.sql b/main/db.sql new file mode 100644 index 0000000..fff923f --- /dev/null +++ b/main/db.sql @@ -0,0 +1,22 @@ +CREATE TABLE messages ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(50) NOT NULL, + message TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + signature TEXT, + is_encrypted BOOLEAN DEFAULT FALSE +); + +CREATE TABLE users ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(50) NOT NULL UNIQUE, + password VARCHAR(255) NOT NULL, + pgp_key TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE registrations ( + id INT AUTO_INCREMENT PRIMARY KEY, + ip VARCHAR(45) NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/main/index.php b/main/index.php new file mode 100644 index 0000000..667d547 --- /dev/null +++ b/main/index.php @@ -0,0 +1,102 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +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) { + $stmt = $db->prepare('INSERT INTO messages (username, message, signature, is_encrypted) VALUES (?, ?, ?, ?)'); + $stmt->execute([$username, $message, $signature, $is_encrypted]); + } + header('Location: ' . $_SERVER['PHP_SELF']); + exit; +} + +$stmt = $db->query('SELECT * FROM messages ORDER BY created_at DESC LIMIT 50'); +$messages = $stmt->fetchAll(PDO::FETCH_ASSOC); +?> + + + + + Text0Nly + + + + + +

Text0Nly

+ +
+

Message API:

+
curl -X POST http://localhost/api.php -H "Content-Type: application/json" -d '{
+    "username": "name",
+    "message": "text",
+    "signature": "pgp_signature",
+    "encrypted": true
+}'
+
+ +
+ + + +
+ + +
+ +
+ +
+ +
+ : +
+ + [Encrypted] + + +
Signature:
+ + +
+ +
+ + + + \ No newline at end of file diff --git a/main/register.php b/main/register.php new file mode 100644 index 0000000..cb22070 --- /dev/null +++ b/main/register.php @@ -0,0 +1,85 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + +$error = ''; +$success = ''; + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING); + $password = $_POST['password'] ?? ''; + $pgp_key = $_POST['pgp_key'] ?? ''; + $ip = $_SERVER['REMOTE_ADDR']; + + if ($username && $password) { + if (strlen($username) > 50 || strlen($password) < 8) { + $error = 'Invalid data'; + } else { + $stmt = $db->prepare('SELECT COUNT(*) FROM registrations WHERE ip = ? AND created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)'); + $stmt->execute([$ip]); + $count = $stmt->fetchColumn(); + + if ($count >= 5) { + $error = 'Registration limit exceeded for your IP'; + } else { + try { + $stmt = $db->prepare('INSERT INTO users (username, password, pgp_key) VALUES (?, ?, ?)'); + $stmt->execute([$username, password_hash($password, PASSWORD_DEFAULT), $pgp_key]); + + $stmt = $db->prepare('INSERT INTO registrations (ip) VALUES (?)'); + $stmt->execute([$ip]); + + $success = 'Registration successful'; + } catch (PDOException $e) { + $error = 'Username already exists'; + } + } + } + } +} +?> + + + + + Text0Nly - Registration + + + +

Registration

+ +
+ + +
+ + +
+
+ +
+
+ +
+
+ +
+ +
+

Back to chat

+ + \ No newline at end of file