diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..7aba0d8 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:$PROJECT_DIR$/storage/database.sqlite + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/bin/WebSocketServer.php b/bin/WebSocketServer.php index a009161..7b61e96 100644 --- a/bin/WebSocketServer.php +++ b/bin/WebSocketServer.php @@ -20,6 +20,7 @@ use \ComCen\Security\TokenHandler; class WebSocketServer implements MessageComponentInterface { private $connectionsData = []; + private $chatGroups = []; private function isSameConnection(ConnectionInterface $connection1, ConnectionInterface $connection2): bool { @@ -61,6 +62,22 @@ class WebSocketServer implements MessageComponentInterface echo "New connection: {$conn->resourceId}\n"; } + public function createGroup(string $owner, string $name, string ...$whitelist): bool + { + foreach ($whitelist as $user) { + if (strlen($user) > 32) { + return false; + } + } + + $chatGroups[] = [ + "owner" => $owner, + "name" => $name, + "whitelist" => $whitelist + ]; + return true; + } + public function onMessage(ConnectionInterface $from, $msg): void { $decodedMsg = json_decode($msg, true); @@ -72,21 +89,32 @@ class WebSocketServer implements MessageComponentInterface if ($index === null) return; if ($this->connectionsData[$index]["username"]) { - $this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $decodedMsg["message"]); - } else { - $token = $decodedMsg["token"] ?? null; - if ($token) { - $tokenUser = TokenHandler::getTokenOwnership($token); - if ($tokenUser) { - $this->connectionsData[$index]["username"] = $tokenUser; - $from->send("authenticated"); - } else { - $from->send("invalid token"); - } - } else { - $from->send("not authenticated"); + $msgContent = $decodedMsg["msg"] ?? null; + if ($msgContent) { + $this->sendToAllAuthenticated($this->connectionsData[$index]["username"], $msgContent); + return; } + + $groupCreationRequest = $decodedMsg["createGroupWithName"] ?? null; + if ($groupCreationRequest) { + + } + + return; } + + $token = $decodedMsg["token"] ?? null; + if ($token) { + $tokenUser = TokenHandler::getTokenOwnership($token); + if ($tokenUser) { + $this->connectionsData[$index]["username"] = $tokenUser; + $from->send("authenticated"); + return; + } + $from->send("invalid token"); + return; + } + $from->send("not authenticated"); } public function onClose(ConnectionInterface $conn): void @@ -113,7 +141,6 @@ $server = IoServer::factory( 8080 ); -Handler::getInstance()->init(); echo "Server running on http://localhost:8080\n"; $server->run(); \ No newline at end of file diff --git a/src/Database/Handler.php b/src/Database/Handler.php index 3a8c807..206eef1 100644 --- a/src/Database/Handler.php +++ b/src/Database/Handler.php @@ -21,16 +21,26 @@ class Handler { $this->pdo = new PDO('sqlite:' . __DIR__ . '/../../storage/database.sqlite'); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - } - - public function init(): void - { $this->pdo->exec(" CREATE TABLE IF NOT EXISTS users ( username VARCHAR(32) NOT NULL UNIQUE, password CHAR(255) NOT NULL ) "); + $this->pdo->exec(" + CREATE TABLE IF NOT EXISTS chat_groups ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(64) NOT NULL UNIQUE + ) + "); + $this->pdo->exec(" + CREATE TABLE IF NOT EXISTS group_members ( + group_id INTEGER NOT NULL, + username VARCHAR(32) NOT NULL, + PRIMARY KEY (group_id, username), + FOREIGN KEY (username) REFERENCES users(username) + ) + "); } public function addUser(string $username, string $password): void diff --git a/storage/users.sqlite b/storage/users.sqlite deleted file mode 100644 index e69de29..0000000