Skip to content

Commit

Permalink
Patched version 1.5 with the changes made to version 1.4.1 fixing #54.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymast committed May 20, 2021
1 parent 7663db1 commit bd9472a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 104 deletions.
15 changes: 11 additions & 4 deletions CHANELOG.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
## 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.
- Fixed some grammar issues.
- 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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>socket_chat</code>.|


***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.



Expand Down
184 changes: 92 additions & 92 deletions includes/classes/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}
Expand Down
13 changes: 6 additions & 7 deletions includes/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit bd9472a

Please sign in to comment.