-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogs.php
More file actions
126 lines (87 loc) · 3 KB
/
Logs.php
File metadata and controls
126 lines (87 loc) · 3 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
<?php
namespace MerapiPanel\Module\Contact;
use MerapiPanel\Box\Module\__Fragment;
class Logs extends __Fragment
{
protected $module;
function onCreate(\MerapiPanel\Box\Module\Entity\Module $module)
{
$this->module = $module;
}
function write($client_ip, $contact_id)
{
$file = __DIR__ . "/logs/" . date("Y-m-d") . ".log";
$logs = $this->read(date("Y-m-d"));
$last = end($logs);
if ($last['client_ip'] == $client_ip && date("Y-m-d H:i:s", strtotime($last['date'])) > date("Y-m-d H:i:s", strtotime("-10 minutes")))
// prevent repeated writes in 10 minutes
{
return false;
}
file_put_contents($file, "[" . date("Y-m-d H:i:s") . "] " . $client_ip . " - " . $contact_id . "\n", FILE_APPEND);
$this->deleteOld(); // delete old logs
}
function read($date)
{
$date = date("Y-m-d", strtotime($date));
$filePath = __DIR__ . "/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 contact_id from each line
preg_match('/\[(.*?)\] (.*?) - (.*?)$/', $line, $matches);
$date = trim($matches[1] ?? '');
$clientIp = trim($matches[2] ?? '');
$contact_id = trim($matches[3] ?? '');
// Create an array representing the log entry
$logEntry = [
"date" => $date,
"client_ip" => $clientIp,
"contact_id" => $contact_id
];
// 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) {
$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__ . "/logs/" . $date . ".log";
if (file_exists($file)) {
unlink($file);
}
}
function deleteOld()
{
$files = glob(__DIR__ . "/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);
}
}
}
}