feat(logs) displaying logs on account page

main
Valentin 2024-06-17 10:59:03 +02:00
parent 387bc0a2d7
commit 3e4cb6ddd9
6 changed files with 46 additions and 13 deletions

View File

@ -71,3 +71,26 @@ $userDetails = getUserDetails($_SESSION['user_id']);
<button type="submit" class="btn btn-primary">Save Changes</button> <button type="submit" class="btn btn-primary">Save Changes</button>
</form> </form>
</div> </div>
<?php
$query = $conn->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);
?>
<table class="table mt-4">
<thead>
<tr>
<th>IP</th>
<th>Date</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php foreach ($logDetails as $log): ?>
<tr>
<td><?php echo $log['ip']; ?></td>
<td><?php echo $log['date']; ?></td>
<td><?php echo $log['email']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

View File

@ -26,6 +26,8 @@ $query = $conn->prepare("SELECT DISTINCT lineId FROM favorites WHERE userId = ?"
$query->execute([$_SESSION['user_id']]); $query->execute([$_SESSION['user_id']]);
$lineIds = $query->fetchAll(PDO::FETCH_COLUMN); $lineIds = $query->fetchAll(PDO::FETCH_COLUMN);
echo $_SERVER['REMOTE_ADDR'];
?> ?>
<div class="px-4 my-5 text-center"> <div class="px-4 my-5 text-center">

View File

@ -13,9 +13,10 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_SESSION['is_admin'] = $user['is_admin']; $_SESSION['is_admin'] = $user['is_admin'];
header("Location: /"); 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(':userId', $user['id']);
$log->bindParam(':date', date('Y-m-d-H-i-s')); $log->bindParam(':date', date('Y-m-d-H-i-s'));
$log->bindParam(':ip', $_SERVER['REMOTE_ADDR']);
$log->execute(); $log->execute();
} else { } else {
$errorMessage = "Invalid email or password."; $errorMessage = "Invalid email or password.";

View File

@ -91,11 +91,11 @@ document.addEventListener('DOMContentLoaded', function() {
if (action === 'add') { if (action === 'add') {
buttonElement.classList.remove('add-stop', 'btn-success'); buttonElement.classList.remove('add-stop', 'btn-success');
buttonElement.classList.add('remove-stop', 'btn-danger'); buttonElement.classList.add('remove-stop', 'btn-danger');
buttonElement.textContent = 'Retirer'; buttonElement.textContent = 'Revoke';
} else { } else {
buttonElement.classList.remove('remove-stop', 'btn-danger'); buttonElement.classList.remove('remove-stop', 'btn-danger');
buttonElement.classList.add('add-stop', 'btn-success'); buttonElement.classList.add('add-stop', 'btn-success');
buttonElement.textContent = 'Ajouter'; buttonElement.textContent = 'Add';
} }
buttonElement.removeEventListener('click', arguments.callee); buttonElement.removeEventListener('click', arguments.callee);
buttonElement.addEventListener('click', arguments.callee); buttonElement.addEventListener('click', arguments.callee);

View File

@ -11,11 +11,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['stopId'], $_POST['lin
try { try {
if ($action === 'add') { if ($action === 'add') {
$stmt = $conn->prepare("INSERT INTO favorites (userId, stopId, lineId) VALUES (?, ?, ?)"); $query = $conn->prepare("SELECT * FROM favorites WHERE userId = ? AND stopId = ? AND lineId = ?");
$stmt->execute([$userId, $stopId, $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') { } elseif ($action === 'remove') {
$stmt = $conn->prepare("DELETE FROM favorites WHERE userId = ? AND stopId = ? AND lineId = ?"); $query = $conn->prepare("DELETE FROM favorites WHERE userId = ? AND stopId = ? AND lineId = ?");
$stmt->execute([$userId, $stopId, $lineId]); $query->execute([$userId, $stopId, $lineId]);
} }
echo json_encode(['success' => true]); echo json_encode(['success' => true]);
} catch (PDOException $e) { } catch (PDOException $e) {

View File

@ -24,6 +24,7 @@ try {
$conn->exec("CREATE TABLE IF NOT EXISTS logs ( $conn->exec("CREATE TABLE IF NOT EXISTS logs (
userId INT NOT NULL, userId INT NOT NULL,
date DATETIME NOT NULL, date DATETIME NOT NULL,
ip VARCHAR(255) NOT NULL,
FOREIGN KEY (userId) REFERENCES users(id) FOREIGN KEY (userId) REFERENCES users(id)
)"); )");