diff --git a/CHANELOG.md b/CHANELOG.md index a5a3c4c..a77048b 100644 --- a/CHANELOG.md +++ b/CHANELOG.md @@ -1,5 +1,7 @@ ## 1.5 Add more security to the chats + - Patched in the database bug that was fixed in release v1.4.1 + fixing #54. (Thanks to @badguyp) - Replaced all defines for cont in includes/config - Updated README.md documentation. - Added Docker support for the package. @@ -7,15 +9,20 @@ - Added dependence in composer for php 7.2 - Added ext-openssl dependence to composer.json +## 1.4.1 Fixed the failing database layer + +- Manually reviewed the database changes. +- Enabling database should now work fixing #54 thanks to @badguyp. +- Fixed a bug in includes/classes/Database.php +- Fixed a bug in includes/classes/Chat.php +- Updated the database import + ## 1.4 Helping developers to build from the project This release will be more about helping developers with useful boiler plate functions. This will assist them to create a new project from this one quickly. -For more information about the project, you can visit our new wiki right here on GitHub. - - - Fixed some grammar issues - - Cleaned up the code according to PSR1 and PSR2 + - Cleaned up the code acording to PSR1 and PSR2 - Added GitHub Actions - Added checks for css via stylelint - Added javascript checks via eslint diff --git a/README.md b/README.md index 88d2163..7a959e1 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ in the [includes/config.php](https://github.com/johnnymast/mysql_websocket_chat/ | DATABASE_DB | Enter the name of the database here. By default this has been set to socket_chat.| -***Please note*** if you enable the database make sure you update the credentials as well (see table above). Also if you enable the database make sure you have imported [database.sql](https://github.com/johnnymast/mysql_websocket_chat/blob/master/database.sql) into your database. +***Please note*** if you enable the database make sure you update the credentials as well (see table above). Also if you enable the database make sure you have imported [database/database.sql](https://github.com/johnnymast/mysql_websocket_chat/blob/master/database/database.sql) into your database. diff --git a/includes/classes/Chat.php b/includes/classes/Chat.php index 20c401a..d601974 100644 --- a/includes/classes/Chat.php +++ b/includes/classes/Chat.php @@ -90,6 +90,7 @@ public function onOpen(ConnectionInterface $conn): void * @param string $msg The message being sent * * @return void + * @throws \Exception */ public function onMessage(ConnectionInterface $from, $msg): void { @@ -105,117 +106,116 @@ public function onMessage(ConnectionInterface $from, $msg): void * build on that later. */ switch ($package->type) { - case 'message': - if ($from !== $client) { - if (empty($package->to_user) == false) { - - /** - * Find the client to send the message to - */ - foreach ($this->users as $resourceId => $user) { - if ($resourceId == $from->resourceId) { - continue; - } + case 'message': + if ($from !== $client) { + if (empty($package->to_user) == false + && isset($package->to_user->id) == true + ) { /** - * Non target users will not see this message - * on their screens. + * Find the client to send the message to */ - if ($user['user']->id == $package->to_user) { - + foreach ($this->users as $resourceId => $user) { /** - * Defined in src/config.php + * Non target users will not see this message + * on their screens. */ - if (ENABLE_DATABASE == true) { - if (isset($package->user) - && is_object($package->user) == true - ) { - /** - * Insert channel chat - */ - $this->db->insert( - $package->to_user->id, - $package->user->id, - $package->message, - $client->remoteAddress - ); + if ($user['user']->id === $package->to_user->id) { + + /** + * Defined in includes/config.php + */ + if (ENABLE_DATABASE == true) { + if (isset($package->user) + && is_object($package->user) == true + ) { + /** + * Insert private chat + */ + $this->db->insert( + $package->to_user->id, + $package->user->id, + $package->message, + $client->remoteAddress + ); + } } - } - $targetClient = $user['client']; - $targetClient->send($msg); - return; + $targetClient = $user['client']; + $targetClient->send($msg); + return; + } } - } - } + } else { - /** - * Defined in src/config.php - */ - if (ENABLE_DATABASE == true) { - if (isset($package->user) - and is_object($package->user) == true - ) { /** - * Insert private chat + * Defined in includes/config.php */ - $this->db->insert( - $package->to_user->id, - $package->user->id, - $package->message, - $client->remoteAddress - ); + if (ENABLE_DATABASE == true) { + if (isset($package->user) + and is_object($package->user) == true + ) { + /** + * Insert channel chat + */ + $this->db->insert( + null, + $package->user->id, + $package->message, + $client->remoteAddress + ); + } + } + $client->send($msg); } } - $client->send($msg); - } - break; - case 'registration': - $this->users[$from->resourceId] = [ - 'user' => $package->user, - 'client' => $from - ]; - break; - case 'userlist': - $list = []; - foreach ($this->users as $resourceId => $value) { - $list[] = $value['user']; - } - $new_package = [ - 'users' => $list, - 'type' => 'userlist' - ]; - $new_package = json_encode($new_package); - $client->send($new_package); - break; - - case 'typing': - if ($from != $client) { - if (empty($package->user) == false) { - /** - * Find the client to send the message to - */ - foreach ($this->users as $resourceId => $user) { - if ($resourceId == $from->resourceId) { - continue; - } + break; + case 'registration': + $this->users[$from->resourceId] = [ + 'user' => $package->user, + 'client' => $from + ]; + break; + case 'userlist': + $list = []; + foreach ($this->users as $resourceId => $value) { + $list[] = $value['user']; + } + $new_package = [ + 'users' => $list, + 'type' => 'userlist' + ]; + $new_package = json_encode($new_package); + $client->send($new_package); + break; + + case 'typing': + if ($from != $client) { + if (empty($package->user) == false) { + /** + * Find the client to send the message to + */ + foreach ($this->users as $resourceId => $user) { + if ($resourceId == $from->resourceId) { + continue; + } - $new_package = [ - 'user' => $package->user, - 'type' => 'typing', - 'value' => $package->value, - ]; + $new_package = [ + 'user' => $package->user, + 'type' => 'typing', + 'value' => $package->value, + ]; - $targetClient = $user['client']; - $targetClient->send($msg); + $targetClient = $user['client']; + $targetClient->send($msg); + } } } - } - break; - default: - throw new \Exception('Unexpected value'); + break; + default: + throw new \Exception('Unexpected value'); break; } } diff --git a/includes/config.php b/includes/config.php index 91662a6..c9ddfed 100644 --- a/includes/config.php +++ b/includes/config.php @@ -16,24 +16,23 @@ date_default_timezone_set('EUROPE/AMSTERDAM'); - - define("DATABASE_HOST", $_ENV['DOCKER_DB_HOST'] ?? 'localhost'); const DATABASE_PORT = 3306; const DATABASE_USERNAME = "root"; const DATABASE_PASSWORD = ""; -const DATABASE_DB = "socket_chat"; +//const DATABASE_DB = "socket_chat"; +const DATABASE_DB = "webchat"; -const ENABLE_DATABASE = false; +const ENABLE_DATABASE = true; const SSL_CERT_BUNDLE = 'ssl/server.pem'; -const ENABLE_SSL = true; +const ENABLE_SSL = false; /** * The host can either be an IP or a hostname * on this machine. The port is just the port * plain and simple. */ -define('WEBSOCKET_SERVER_BIND_IP', $_ENV['DOCKER_WEBSOCKET_BIND_IP'] ?? '127.0.0.1'); -define("WEBSOCKET_SERVER_IP", '192.168.178.21'); +define('WEBSOCKET_SERVER_BIND_IP', $_ENV['DOCKER_WEBSOCKET_BIND_IP'] ?? '192.168.178.119'); +define("WEBSOCKET_SERVER_IP", '192.168.178.119'); define("WEBSOCKET_SERVER_PORT", '8090');