From 96502caea40e905661d660231f20b9d238b4fc56 Mon Sep 17 00:00:00 2001 From: kohlicekjan Date: Tue, 25 Sep 2018 20:49:03 +0200 Subject: [PATCH] =?UTF-8?q?Vy=C5=99e=C5=A1en=C3=AD=20z=C3=A1kladn=C3=ADho?= =?UTF-8?q?=20zad=C3=A1n=C3=AD,=202.=20Roz=C5=A1=C3=AD=C5=99en=C3=A9=20zad?= =?UTF-8?q?=C3=A1n=C3=AD=20-=20Autentikace=20a=20p=C5=99idan=C3=A1=20mo?= =?UTF-8?q?=C5=BEnost=20odeslat=20zpr=C3=A1vu=20pomoc=C3=AD=20kl=C3=A1vesy?= =?UTF-8?q?=20enter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.html | 7 +++++++ src/init.php | 11 ++++++----- src/login.php | 20 ++++++++++---------- src/logout.php | 7 +++---- src/sendMessage.php | 7 ++++--- src/showMessages.php | 31 +++++++++++++++++-------------- 6 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/index.html b/src/index.html index 001ad26..eef1437 100644 --- a/src/index.html +++ b/src/index.html @@ -42,6 +42,13 @@

Redis chat demo

}); }); + text.addEventListener("keyup", function (event) { + event.preventDefault(); + if (event.keyCode === 13) { + sendBtn.click(); + } + }); + setInterval(function(){ xhr('GET', 'showMessages.php', null, function(response){ diff --git a/src/init.php b/src/init.php index 8dcd708..018ec58 100644 --- a/src/init.php +++ b/src/init.php @@ -5,13 +5,13 @@ $redis = new Predis\Client([ 'scheme' => 'tcp', - 'host' => 'redis', + 'host' => 'localhost', 'port' => 6379, + 'async' => false, + 'throw_errors' => true ]); function loadCurentUserId($authSecret) { - return 1; // EXTENDED TASK: delete this line to complete the extended task - global $redis; // empty auth secret means the user is logged out @@ -20,10 +20,11 @@ function loadCurentUserId($authSecret) { } // use the auth secret to get the user ID - // $userId = _____________ (EXTENDED TASK) + $userId = $redis->executeRaw(["GET", "authSecret_$authSecret"]); + if ($userId) { // cross check that this auth secret is also stored in the user hash - // $userAuthSecret = _____________ (EXTENDED TASK) + $userAuthSecret = $redis->executeRaw(["HGET", $userId, "authSecret"]); if ($userAuthSecret != $authSecret) { return null; } diff --git a/src/login.php b/src/login.php index e319f30..1fdaa9e 100644 --- a/src/login.php +++ b/src/login.php @@ -1,18 +1,16 @@ executeRaw(["GET", "user_$username"]); if ($userId) { // user ID exists => continue with the login flow - // $realPassword = __________________ (EXTENDED TASK) + $realPassword = $redis->executeRaw(["HGET", $userId, "password"]); if ($password === $realPassword) { doLogin($userId); } else { @@ -23,11 +21,13 @@ } else { // user ID does not exist => continue with the register flow // obtain new user ID - // $userId = _________________ (EXTENDED TASK) + $userId = "user_" . $redis->executeRaw(["INCR", "user_sequence"]); + // store this user account into a hash - // ________________________ (EXTENDED TASK) + $redis->executeRaw(["HMSET", $userId, "username", $username, "password", "$password"]); + // store the user ID into a hash - this is needed to lookup user IDs by usernames - // ________________________ (EXTENDED TASK) + $redis->executeRaw(["SET", "user_$username", $userId]); // login the user doLogin($userId); @@ -41,13 +41,13 @@ function doLogin($userId) { $authSecret = hash('sha256', $rand); // delete the old auth secret (in case it exists) - // ________________________ (EXTENDED TASK) + $redis->executeRaw(["HDEL", $userId, "authSecret"]); // update the auth secret stored in the user hash - // ________________________ (EXTENDED TASK) + $redis->executeRaw(["HSET", $userId, "authSecret", $authSecret]); // store the user ID into a hash - this is needed to lookup user IDs by user secrets - // ________________________ (EXTENDED TASK) + $redis->executeRaw(["SET", "authSecret_$authSecret", $userId]); setcookie("auth", $authSecret, time() + 3600 * 24 * 365); } diff --git a/src/logout.php b/src/logout.php index d1ff7f2..48e7c16 100644 --- a/src/logout.php +++ b/src/logout.php @@ -1,7 +1,5 @@ executeRaw(["HDEL", $userId, "authSecret"]); + // set this user's auth secret to empty -// ___________________ (EXTENDED TASK) +$redis->executeRaw(["DEL", "authSecret_$authSecret"]); setcookie('auth', '', 1); diff --git a/src/sendMessage.php b/src/sendMessage.php index 9254db1..0f7888a 100644 --- a/src/sendMessage.php +++ b/src/sendMessage.php @@ -13,10 +13,11 @@ $text = $_POST['text']; // get the ID of the message -// $messageId = _______________ (BASIC TASK) +$messageId = "message_" . $redis->executeRaw(["INCR", "message_sequence"]); // insert the message into its own hash -// _______________ (BASIC TASK) +$username = $redis->executeRaw(["HGET", $userId, "username"]); +$redis->executeRaw(["HMSET", $messageId, "time", $time, "text", "$text", "username", $username]); // push the message into the list of message IDs -// _______________ (BASIC TASK) +$redis->executeRaw(["RPUSH", "messages", $messageId]); diff --git a/src/showMessages.php b/src/showMessages.php index 338f694..a5b0880 100644 --- a/src/showMessages.php +++ b/src/showMessages.php @@ -10,27 +10,30 @@ } // get 10 latest messages -// $messages = _______________ (BASIC TASK) +$messages = $redis->executeRaw(["LRANGE", "messages", "0", "10"]); +$messages = array_reverse($messages); -foreach ($messages as $id) { +foreach ($messages as $messageId) { // get all properties of the message - // $message = _______________ (BASIC TASK) + $message = []; + $message["text"] = $redis->executeRaw(["HGET", $messageId, "text"]); + $message["time"] = $redis->executeRaw(["HGET", $messageId, "time"]); // add the author's username to the message array - $message['username'] = 'Anonymous'; - // $message['username'] = _____________ (EXTENDED TASK) + $message['username'] = $redis->executeRaw(["HGET", $messageId, "username"]); printMessage($message); } -function printMessage(array $message) { +function printMessage(array $message) +{ echo "" - ."" - ."" - ."" - ."" - ."" - ."" - ."" - ."
" . $message['username'] . "". date('m/d/Y H:i:s', $message['time']) ."
". $message['text'] ."
"; + . "" + . "" . $message['username'] . "" + . "" . date('m/d/Y H:i:s', $message['time']) . "" + . "" + . "" + . "" . $message['text'] . "" + . "" + . ""; }