-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogs.php
More file actions
144 lines (104 loc) · 3.85 KB
/
Logs.php
File metadata and controls
144 lines (104 loc) · 3.85 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace MerapiPanel\Module\Website;
use MerapiPanel\Box\Module\__Fragment;
class Logs extends __Fragment
{
protected $module;
function onCreate(\MerapiPanel\Box\Module\Entity\Module $module)
{
$this->module = $module;
if (!file_exists(__DIR__ . "/data/logs/")) {
mkdir(__DIR__ . "/data/logs/", 0777, 1);
}
}
function write($client_ip, $page_path, $page_title = "")
{
$file = __DIR__ . "/data/logs/" . date("Y-m-d") . ".log";
$logs = $this->read(date("Y-m-d"));
if (!empty($logs)) {
$filtered = array_filter($logs, function ($log) use ($client_ip, $page_path) {
return $log['client_ip'] == $client_ip
&& preg_replace("/[^a-z0-9]/i", "", strtolower($log['page_path'])) == preg_replace("/[^a-z0-9]/i", "", strtolower($page_path));
});
if (count($filtered) > 0) {
// sort by date
array_multisort(array_column($filtered, 'date'), SORT_DESC, $filtered);
$last = end($filtered);
$last_date = $last['date'];
if (strtotime($last_date) > strtotime("-10 minutes"))
// prevent repeated writes in 10 minutes
{
return false;
}
}
}
file_put_contents($file, "[" . date("Y-m-d H:i:s") . "] " . $client_ip . " - " . $page_path . " | " . $page_title . "\n", FILE_APPEND);
$this->deleteOld(); // delete old logs
}
function read($date)
{
$date = date("Y-m-d", strtotime($date));
$filePath = __DIR__ . "/data/logs/" . $date . ".log";
if (!file_exists($filePath)) {
return [];
}
// Open the file for reading
$fileHandle = fopen($filePath, "r");
// Initialize an empty array to store log entries
$logEntries = [];
// Read each line of the file
while (($line = fgets($fileHandle)) !== false) {
// Extract date, client IP, and page from each line
preg_match('/\[(.*?)\] (.*?) - (.*?) | (.*?)$/', $line, $matches);
$date = trim($matches[1] ?? '');
$clientIp = trim($matches[2] ?? '');
$page_path = trim($matches[3] ?? '');
$page_title = trim($matches[4] ?? '');
// Create an array representing the log entry
$logEntry = [
"date" => $date,
"client_ip" => $clientIp,
"page_path" => $page_path,
"page_title" => $page_title
];
// Add the log entry to the array
$logEntries[] = $logEntry;
}
// Close the file handle
fclose($fileHandle);
// Return the array of log entries
return $logEntries;
}
function readRange($start, $end)
{
$start = date("Y-m-d", strtotime($start));
$end = date("Y-m-d", strtotime($end));
$scan_date = $start;
$data = [];
while ($scan_date <= $end) {
$file = __DIR__ . "/data/logs/" . $scan_date . ".log";
$name = date("M d", strtotime($scan_date));
$data[$name] = $this->read($scan_date);
$scan_date = date("Y-m-d", strtotime($scan_date . " +1 day"));
}
return $data;
}
function delete($date)
{
$date = date("Y-m-d", strtotime($date));
$file = __DIR__ . "/data/logs/" . $date . ".log";
if (file_exists($file)) {
unlink($file);
}
}
function deleteOld()
{
$files = glob(__DIR__ . "/data/logs/*");
foreach ($files as $file) {
// delete files older than 1 months
if (date("Y-m-d", filemtime($file)) < date("Y-m-d", strtotime("-1 months"))) {
unlink($file);
}
}
}
}