From 3e4cb6ddd9d385b2b9edd6fdadfd919b8837e2d5 Mon Sep 17 00:00:00 2001 From: Valentin <43043885+itsmrval@users.noreply.github.com> Date: Mon, 17 Jun 2024 10:59:03 +0200 Subject: [PATCH] feat(logs) displaying logs on account page --- components/account/main.php | 33 ++++++++++++++++++++++++++++----- components/homepage/main.php | 2 ++ components/login/main.php | 5 +++-- components/navigate/main.php | 4 ++-- public/updateFavorite.php | 14 ++++++++++---- services/db.php | 1 + 6 files changed, 46 insertions(+), 13 deletions(-) diff --git a/components/account/main.php b/components/account/main.php index 62cf702..5caa7e8 100644 --- a/components/account/main.php +++ b/components/account/main.php @@ -48,9 +48,9 @@ $userDetails = getUserDetails($_SESSION['user_id']); -

Edit Account

-
+ ?> +

Edit Account

+
@@ -69,5 +69,28 @@ $userDetails = getUserDetails($_SESSION['user_id']); Leave blank if you do not want to change the password
-
- \ No newline at end of file + + +prepare("SELECT logs.ip, logs.date, users.email FROM logs JOIN users ON logs.userId = users.id"); +$query->execute(); +$logDetails = $query->fetchAll(PDO::FETCH_ASSOC); +?> + + + + + + + + + + + + + + + + + +
IPDateEmail
diff --git a/components/homepage/main.php b/components/homepage/main.php index e0d0355..d757065 100644 --- a/components/homepage/main.php +++ b/components/homepage/main.php @@ -26,6 +26,8 @@ $query = $conn->prepare("SELECT DISTINCT lineId FROM favorites WHERE userId = ?" $query->execute([$_SESSION['user_id']]); $lineIds = $query->fetchAll(PDO::FETCH_COLUMN); +echo $_SERVER['REMOTE_ADDR']; + ?>
diff --git a/components/login/main.php b/components/login/main.php index 3c2ff8d..9837f1b 100644 --- a/components/login/main.php +++ b/components/login/main.php @@ -12,10 +12,11 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $_SESSION['first_name'] = $user['first_name']; $_SESSION['is_admin'] = $user['is_admin']; header("Location: /"); - - $log = $conn->prepare("INSERT INTO logs (userId, date) VALUES (:userId, :date)"); + + $log = $conn->prepare("INSERT INTO logs (userId, date, ip) VALUES (:userId, :date, :ip)"); $log->bindParam(':userId', $user['id']); $log->bindParam(':date', date('Y-m-d-H-i-s')); + $log->bindParam(':ip', $_SERVER['REMOTE_ADDR']); $log->execute(); } else { $errorMessage = "Invalid email or password."; diff --git a/components/navigate/main.php b/components/navigate/main.php index 898b8bd..fb049c2 100644 --- a/components/navigate/main.php +++ b/components/navigate/main.php @@ -91,11 +91,11 @@ document.addEventListener('DOMContentLoaded', function() { if (action === 'add') { buttonElement.classList.remove('add-stop', 'btn-success'); buttonElement.classList.add('remove-stop', 'btn-danger'); - buttonElement.textContent = 'Retirer'; + buttonElement.textContent = 'Revoke'; } else { buttonElement.classList.remove('remove-stop', 'btn-danger'); buttonElement.classList.add('add-stop', 'btn-success'); - buttonElement.textContent = 'Ajouter'; + buttonElement.textContent = 'Add'; } buttonElement.removeEventListener('click', arguments.callee); buttonElement.addEventListener('click', arguments.callee); diff --git a/public/updateFavorite.php b/public/updateFavorite.php index a054af9..7411773 100644 --- a/public/updateFavorite.php +++ b/public/updateFavorite.php @@ -11,11 +11,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['stopId'], $_POST['lin try { if ($action === 'add') { - $stmt = $conn->prepare("INSERT INTO favorites (userId, stopId, lineId) VALUES (?, ?, ?)"); - $stmt->execute([$userId, $stopId, $lineId]); + $query = $conn->prepare("SELECT * FROM favorites WHERE userId = ? AND stopId = ? AND lineId = ?"); + $query->execute([$userId, $stopId, $lineId]); + $existingFavorite = $query->fetch(); + + if (!$existingFavorite) { + $query = $conn->prepare("INSERT INTO favorites (userId, stopId, lineId) VALUES (?, ?, ?)"); + $query->execute([$userId, $stopId, $lineId]); + } } elseif ($action === 'remove') { - $stmt = $conn->prepare("DELETE FROM favorites WHERE userId = ? AND stopId = ? AND lineId = ?"); - $stmt->execute([$userId, $stopId, $lineId]); + $query = $conn->prepare("DELETE FROM favorites WHERE userId = ? AND stopId = ? AND lineId = ?"); + $query->execute([$userId, $stopId, $lineId]); } echo json_encode(['success' => true]); } catch (PDOException $e) { diff --git a/services/db.php b/services/db.php index 00eb329..b986b7e 100644 --- a/services/db.php +++ b/services/db.php @@ -24,6 +24,7 @@ try { $conn->exec("CREATE TABLE IF NOT EXISTS logs ( userId INT NOT NULL, date DATETIME NOT NULL, + ip VARCHAR(255) NOT NULL, FOREIGN KEY (userId) REFERENCES users(id) )");