-
Notifications
You must be signed in to change notification settings - Fork 2
/
logs.php
115 lines (97 loc) · 3.38 KB
/
logs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
define('SENTINEL_WEB_PAGE_TO_ROOT', '');
require_once SENTINEL_WEB_PAGE_TO_ROOT . 'sentinel/includes/sentinelPage.inc.php';
sentinelPageStartup(array('authenticated'));
sentinelDatabaseConnect();
/*
//This whole code should be run in every page to get the log of every page view
// Function to get the user's IP address
function getUserIP() {
$ip = '';
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
$ip = $_SERVER['HTTP_X_FORWARDED'];
} elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_FORWARDED'])) {
$ip = $_SERVER['HTTP_FORWARDED'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
// Function to insert a log entry
function insertLogEntry($ip, $visited, $time) {
$query = "INSERT INTO logs (ip, visited, time) VALUES ('$ip', '$visited', '$time')";
mysqli_select_db($GLOBALS["___mysqli_ston"], "sentinel" );
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query);
if (!$result) {
die("Failed to insert log entry: " . mysqli_error($GLOBALS["___mysqli_ston"]));
}
}
// Test: Insert log entry when the page is accessed
//$user = isset($_SESSION['user']) ? $_SESSION['user'] : 'guest'; // Replace with your actual user data
$ip = getUserIP();
$visited = $_SERVER['REQUEST_URI'];
$time = date("Y-m-d H:i:s");
insertLogEntry($ip, $visited, $time);
*/
$page = sentinelPageNewGrab();
$page['title'] = 'Log Viewer';
$page['page_id'] = 'log_viewer';
// Fetch logs from the 'logs' table
$query = "SELECT * FROM logs";
mysqli_select_db($GLOBALS["___mysqli_ston"], "sentinel");
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query);
// Fetch logs count from the 'logs' table
$countQuery = "SELECT COUNT(*) as log_count FROM logs";
mysqli_select_db($GLOBALS["___mysqli_ston"], "sentinel");
$countResult = mysqli_query($GLOBALS["___mysqli_ston"], $countQuery);
$logCount = ($countResult) ? mysqli_fetch_assoc($countResult)['log_count'] : 0;
// Clear button logic
if (isset($_POST['clear_logs'])) {
$clearQuery = "TRUNCATE TABLE logs";
mysqli_query($GLOBALS["___mysqli_ston"], $clearQuery);
sentinelMessagePush('All log records cleared.');
sentinelPageReload();
}
$logHtml = '<h2>Log Records</h2>';
$logHtml .= '<table border="10px" style="border-color: black;">
<tr>
<th>Log ID</th>
<th>User</th>
<th>IP</th>
<th>Visited Page</th>
<th>Time</th>
</tr>';
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$logHtml .= "<tr>
<td>{$row['log_id']}</td>
<td>{$row['user']}</td>
<td>{$row['ip']}</td>
<td>{$row['visited']}</td>
<td>{$row['time']}</td>
</tr>";
}
mysqli_free_result($result);
} else {
$logHtml .= '<tr><td colspan="5">No Records Found</td></tr>';
}
$logHtml .= '</table>';
$page['body'] = "
<div class=\"body_padded\">
<h1>Log Viewer</h1>
<br>
{$logCount} Log Records Found
<form action=\"#\" method=\"POST\">
<input type=\"submit\" value=\"Clear\" name=\"clear_logs\">
</form>
<br />
{$logHtml}
</div>";
sentinelHtmlEcho($page);
?>