Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2'
version: '2.1'
services:
apache-php:
image: nimmis/apache-php7
Expand Down
10 changes: 5 additions & 5 deletions src/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
]);

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
Expand All @@ -20,10 +18,12 @@ function loadCurentUserId($authSecret) {
}

// use the auth secret to get the user ID
// $userId = _____________ (EXTENDED TASK)
if ($userId) {
$userId = $redis->hget("users", $authSecret);

if (isset($userId)) {
// cross check that this auth secret is also stored in the user hash
// $userAuthSecret = _____________ (EXTENDED TASK)
$userAuthSecret = $redis->hget("user:" . $userId, "authSecret");

if ($userAuthSecret != $authSecret) {
return null;
}
Expand Down
86 changes: 52 additions & 34 deletions src/login.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,71 @@
<?php

exit; // EXTENDED TASK: delete this line to complete the extended task

require "init.php";

$username = $_POST['username'];
$password = $_POST['password'];

// lookup the user IDs by username
// $userId = ___________________ (EXTENDED TASK)
try{
$userId = $redis->hget("users", $username);
}
catch(Exception $e){
$userId = null;
}

if ($userId) {
// user ID exists => continue with the login flow
// $realPassword = __________________ (EXTENDED TASK)
if ($password === $realPassword) {
doLogin($userId);
} else {
http_response_code(401);
echo 'This account already exists and entered password is incorrect!';
exit;
}
// user ID exists => continue with the login flow
$realPassword = $redis->hget("user:" . $userId, "password");

if ($password === $realPassword) {
doLogin($userId);
} else {
http_response_code(401);
echo 'This account already exists and entered password is incorrect!';
exit;
}
} else {
// user ID does not exist => continue with the register flow
// obtain new user ID
// $userId = _________________ (EXTENDED TASK)
// store this user account into a hash
// ________________________ (EXTENDED TASK)
// store the user ID into a hash - this is needed to lookup user IDs by usernames
// ________________________ (EXTENDED TASK)

// login the user
doLogin($userId);

// user ID does not exist => continue with the register flow
// obtain new user ID
try{
$last = $redis->llen("user_ids");
}
catch(Exception $e){
$last = 0;
}
$userId = $last + 1;

// store this user account into a hash
$redis->lpush("user_ids", array($userId));

// store the user ID into a hash - this is needed to lookup user IDs by usernames
$redis->hmset("user:$userId", array(
"username" => "$username",
"password" => "$password",
));

$redis->hset("users", $username, $userId);

// login the user
doLogin($userId);
}

function doLogin($userId) {
global $redis;
global $redis;

// calculate random user secret
$rand = rand(0, PHP_INT_MAX) . $userId;
$authSecret = hash('sha256', $rand);
// calculate random user secret
$rand = rand(0, PHP_INT_MAX) . $userId;
$authSecret = hash('sha256', $rand);

// delete the old auth secret (in case it exists)
// ________________________ (EXTENDED TASK)
// delete the old auth secret (in case it exists)
$redis->hdel("users", array($authSecret));

// update the auth secret stored in the user hash
// ________________________ (EXTENDED TASK)
// update the auth secret stored in the user hash
$redis->hset("user:" . $userId, "authSecret", $authSecret);

// store the user ID into a hash - this is needed to lookup user IDs by user secrets
// ________________________ (EXTENDED TASK)
// store the user ID into a hash - this is needed to lookup user IDs by user secrets
$redis->hset("users", $authSecret, $userId);

setcookie("auth", $authSecret, time() + 3600 * 24 * 365);
}
setcookie("auth", $authSecret, time() + 3600 * 24 * 365);
}
13 changes: 8 additions & 5 deletions src/logout.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

exit; // EXTENDED TASK: delete this line to complete the extended task

require "init.php";

$userId = loadCurentUserId($_COOKIE['auth']);
Expand All @@ -11,9 +9,14 @@
exit;
}

// delete the old auth secret
// ___________________ (EXTENDED TASK)
// delete the old auth secret (in case it exists)
$authSecret = $redis->hget("user" . $userId, "authSecret");
if($authSecret){
$redis->hdel("users", array($authSecret));
}

// set this user's auth secret to empty
// ___________________ (EXTENDED TASK)
$redis->hset("user:" . $userId, "authSecret", "");


setcookie('auth', '', 1);
11 changes: 8 additions & 3 deletions src/sendMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
$text = $_POST['text'];

// get the ID of the message
// $messageId = _______________ (BASIC TASK)
$last = $redis->llen("messages");
$messageId = ++$last;

// insert the message into its own hash
// _______________ (BASIC TASK)
$redis->hmset("message:" . $messageId, array(
"time" => $time,
"text" => $text,
"userId" => $userId
));

// push the message into the list of message IDs
// _______________ (BASIC TASK)
$redis->lpush("messages", array($messageId));
15 changes: 10 additions & 5 deletions src/showMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
}

// get 10 latest messages
// $messages = _______________ (BASIC TASK)
$messages = $redis->lrange("messages", 0, 10);

foreach ($messages as $id) {
// get all properties of the message
// $message = _______________ (BASIC TASK)
$message = $redis->hgetall("message:" . $id);


if(isset($message["userId"])){
// add the author's username to the message array
$message['username'] = $redis->hget("user:" . $message["userId"], "username");
} else {
$message['username'] = "Annonymous";
}

// add the author's username to the message array
$message['username'] = 'Anonymous';
// $message['username'] = _____________ (EXTENDED TASK)

printMessage($message);
}
Expand Down