diff --git a/CHANELOG.md b/CHANELOG.md index 6dbeed9..5ae8140 100644 --- a/CHANELOG.md +++ b/CHANELOG.md @@ -1,3 +1,10 @@ +## 1.4.1 Fixed the failing database layer + +- 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 diff --git a/README.md b/README.md index 3cabbed..6f7b85b 100644 --- a/README.md +++ b/README.md @@ -63,13 +63,14 @@ in the [includes/config.php](https://github.com/johnnymast/mysql_websocket_chat/ | Flag | Description | | --- | --- | | DATABASE_HOST | The database username goes in here. By default this has been set to root. | +| DATABASE_PORT | The database port goes in here. By default this has been set to 3306. | | DATABASE_USERNAME | The database username goes in here. By default this has been set to root.| | DATABASE_PASSWORD | Enter the password to access the database there. By default this has been set to root.| | DATABASE_DB | Enter the name of the database here. By default this has been set to socket_chat.| | ENABLE_DATABASE | This flag will turn using the database on or off by setting its value to true or false.| -***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.sql](https://github.com/johnnymast/mysql_websocket_chat/blob/master/database.sql) into your database. # Step 4: Fire up the WebSocket server @@ -127,7 +128,7 @@ Oh and if you've come down this far, you might as well [follow me](https://twitt MIT License -Copyright (c) 2020 Johnny Mast +Copyright (c) 2021 Johnny Mast Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/database.sql b/database.sql index bb4f50e..a1927b0 100644 --- a/database.sql +++ b/database.sql @@ -1,50 +1,46 @@ + -- phpMyAdmin SQL Dump --- version 4.4.10 --- http://www.phpmyadmin.net +-- version 5.1.0 +-- https://www.phpmyadmin.net/ -- --- Host: localhost:3306 --- Generation Time: Sep 18, 2016 at 03:50 PM --- Server version: 5.5.42 --- PHP Version: 7.0.0 +-- Host: db +-- Gegenereerd op: 27 apr 2021 om 17:26 +-- Serverversie: 10.3.28-MariaDB-1:10.3.28+maria~focal +-- PHP-versie: 7.4.16 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +START TRANSACTION; SET time_zone = "+00:00"; + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + -- -- Database: `socket_chat` -- +CREATE DATABASE IF NOT EXISTS `socket_chat` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci; +USE `socket_chat`; -- -------------------------------------------------------- -- --- Table structure for table `chat_interactions` +-- Tabelstructuur voor tabel `chat_interactions` -- -CREATE TABLE `chat_interactions` ( - `message_id` int(11) NOT NULL, - `to_id` varchar(255) NOT NULL, - `from_id` varchar(255) NOT NULL, - `message` text NOT NULL, - `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - `ip_address` varchar(255) NOT NULL -) ENGINE=InnoDB DEFAULT CHARSET=latin1; +CREATE TABLE IF NOT EXISTS `chat_interactions` ( + `message_id` int(11) NOT NULL AUTO_INCREMENT, + `to_id` varchar(255) DEFAULT NULL, + `from_id` varchar(255) NOT NULL, + `message` text NOT NULL, + `time` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), + `ip_address` varchar(255) NOT NULL, + PRIMARY KEY (`message_id`) + ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; --- --- Indexes for dumped tables --- --- --- Indexes for table `chat_interactions` --- -ALTER TABLE `chat_interactions` - ADD PRIMARY KEY (`message_id`); - --- --- AUTO_INCREMENT for dumped tables --- - --- --- AUTO_INCREMENT for table `chat_interactions` --- -ALTER TABLE `chat_interactions` - MODIFY `message_id` int(11) NOT NULL AUTO_INCREMENT; \ No newline at end of file +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; \ No newline at end of file diff --git a/includes/classes/Chat.php b/includes/classes/Chat.php index 124959f..d679605 100644 --- a/includes/classes/Chat.php +++ b/includes/classes/Chat.php @@ -128,8 +128,11 @@ public function onMessage(ConnectionInterface $from, $msg): void if (isset($package->user) and is_object($package->user) == true ) { + /** + * Insert channel chat + */ $this->db->insert( - $package->to_user, + $package->to_user->id, $package->user->id, $package->message, $client->remoteAddress @@ -152,8 +155,11 @@ public function onMessage(ConnectionInterface $from, $msg): void if (isset($package->user) and is_object($package->user) == true ) { + /** + * Insert private chat + */ $this->db->insert( - $package->to_user, + $package->to_user->id, $package->user->id, $package->message, $client->remoteAddress diff --git a/includes/classes/Database.php b/includes/classes/Database.php index 76fcbf5..48fbed3 100644 --- a/includes/classes/Database.php +++ b/includes/classes/Database.php @@ -4,14 +4,14 @@ * * The main configuration file for mysql_websocket_chat * - * PHP version 7.2 + * PHP version 7.2 and up. * * @category Configuration * @package Mysql_Websocket_Chat * @author Johnny Mast * @license https://opensource.org/licenses/MIT MIT * @link https://github.com/johnnymast/mysql_websocket_chat - * @since GIT:1.0 + * @since 1.0 */ /** @@ -24,22 +24,27 @@ * @author Johnny Mast * @license https://opensource.org/licenses/MIT MIT * @link https://github.com/johnnymast/mysql_websocket_chat - * @since GIT:1.0 + * @since 1.0 */ -class Database extends PDO +class Database extends \PDO { /** * Database constructor. * - * @param string $username The username for the database - * @param string $password The password for the database - * @param string $host The hostname for the database - * @param string $db The database name + * @param string $username The username for the database + * @param string $password The password for the database + * @param string $host The hostname for the database + * @param integer $port The port for the database + * @param string $db The database name */ - public function __construct($username = '', $password = '', $host = '', $db = '') - { - $dsn = 'mysql:dbname='.$db.';host='.$host; + public function __construct($username = '', + $password = '', + $host = '', + $port = 3306, + $db = '' + ) { + $dsn = 'mysql:dbname=' . $db . ';host=' . $host . ':' . $port; parent::__construct($dsn, $username, $password); } @@ -68,14 +73,13 @@ public function insert( ip_address = :ip_address" ); - $statement->execute( [ - 'to_id' => $to_id, - 'from_id' => $from_id, - 'message' => $message, - 'ip_address' => $ip_address + 'to_id' => $to_id, + 'from_id' => $from_id, + 'message' => $message, + 'ip_address' => $ip_address ] ); } -} \ No newline at end of file +} diff --git a/includes/config.php b/includes/config.php index 1691db6..bb0beba 100644 --- a/includes/config.php +++ b/includes/config.php @@ -18,11 +18,13 @@ date_default_timezone_set('EUROPE/AMSTERDAM'); define('DATABASE_HOST', 'localhost'); +define('DATABASE_PORT', 3306); define('DATABASE_USERNAME', 'root'); define('DATABASE_PASSWORD', 'root'); define('DATABASE_DB', 'socket_chat'); define('ENABLE_DATABASE', false); + /** * The host can either be an IP or a hostname * on this machine. The port is just the port diff --git a/server.php b/server.php index 56e1b89..6d21ae6 100644 --- a/server.php +++ b/server.php @@ -8,7 +8,6 @@ use Ratchet\Http\HttpServer; use Ratchet\WebSocket\WsServer; - /** * Create a new connection to * the database that we can inject @@ -20,6 +19,7 @@ DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_HOST, + DATABASE_PORT, DATABASE_DB ); } else {