-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunctions.php
More file actions
94 lines (74 loc) · 2.4 KB
/
Copy pathfunctions.php
File metadata and controls
94 lines (74 loc) · 2.4 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
<?php
session_start();
define('DASHBOARD_VERSION', trim(@file_get_contents(__DIR__ . '/VERSION')) ?: 'dev');
$configFile = 'config.php';
$defaultConfig = [
'gateway_log_file' => 'files/dashboard.log',
'gateway_config_file' => 'files/m17-gateway.ini',
'hostfile' => 'files/M17Hosts.txt',
'override_hostfile' => 'files/OverrideHosts.txt',
'maxlines' => '15',
'sms_max' => '20',
'timezone' => 'UTC',
'unit_system' => 'metric',
'map_marker_ttl' => '43200',
];
if (!isset($_SESSION['radio_status'])) {
$_SESSION['radio_status'] = 'Listening';
}
// Create the config file if it doesn't exist
if (!file_exists($configFile)) {
file_put_contents($configFile, "<?php\nreturn " . var_export($defaultConfig, true) . ";\n");
$config = $defaultConfig;
} else {
$config = include $configFile;
// Add missing keys with default values
$updated = false;
foreach ($defaultConfig as $key => $value) {
if (!array_key_exists($key, $config)) {
$config[$key] = $value;
$updated = true;
}
}
// If updates were made, rewrite the config file
if ($updated) {
file_put_contents($configFile, "<?php\nreturn " . var_export($config, true) . ";\n");
}
}
// just read the latest 50 lines from the logfile
// while not touching the rest of the file
function tailFile($filePath, $lines = 50) {
$f = fopen($filePath, "r");
if (!$f) return false;
$buffer = '';
$chunkSize = 4096; // Read 4KB at a time
$pos = -1;
$lineCount = 0;
$fileSize = filesize($filePath);
if ($fileSize === 0) {
fclose($f);
return [];
}
fseek($f, 0, SEEK_END);
while (ftell($f) > 0 && $lineCount <= $lines) {
$readSize = ($fileSize - abs($pos) < $chunkSize) ? $fileSize - abs($pos) : $chunkSize;
// Ensure readSize is never zero or negative
if ($readSize <= 0) {
break;
}
$pos -= $readSize;
fseek($f, $pos, SEEK_END);
$data = fread($f, $readSize);
if ($data === false) {
break; // Stop on fread failure
}
$buffer = $data . $buffer;
$lineCount = substr_count($buffer, "\n");
if (abs($pos) >= $fileSize) {
break; // Stop if we've reached the beginning of the file
}
}
fclose($f);
$linesArray = explode("\n", $buffer);
return array_slice($linesArray, -$lines);
}