Skip to content

Commit 101eed9

Browse files
committed
scope the file and folder identifiers to the local machine IP address
1 parent c20cc0a commit 101eed9

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

src/LogFile.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(string $path, ?string $type = null)
2828
{
2929
$this->path = $path;
3030
$this->name = basename($path);
31-
$this->identifier = Utils::shortMd5($path).'-'.$this->name;
31+
$this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path).'-'.$this->name;
3232
$this->type = $type;
3333

3434
// Let's remove the file name because we already know it.
@@ -94,7 +94,7 @@ public function sizeFormatted(): string
9494

9595
public function subFolderIdentifier(): string
9696
{
97-
return Utils::shortMd5($this->subFolder);
97+
return Utils::shortMd5(Utils::getLocalIP().':'.$this->subFolder);
9898
}
9999

100100
public function downloadUrl(): string

src/LogFolder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(
1616
public string $path,
1717
mixed $files,
1818
) {
19-
$this->identifier = Utils::shortMd5($path);
19+
$this->identifier = Utils::shortMd5(Utils::getLocalIP().':'.$path);
2020
$this->files = new LogFileCollection($files);
2121
}
2222

src/Utils/Utils.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class Utils
88
{
9+
private static string $_cachedLocalIP;
10+
911
/**
1012
* Get a human-friendly readable string of the number of bytes provided.
1113
*/
@@ -85,4 +87,37 @@ public static function glob_recursive($pattern, $flags = 0): array
8587

8688
return $files;
8789
}
90+
91+
public static function getLocalIP(bool $cached = true): string
92+
{
93+
if (isset(self::$_cachedLocalIP) && $cached) {
94+
return self::$_cachedLocalIP;
95+
}
96+
97+
if (isset($_SERVER['SERVER_ADDR'])) {
98+
self::$_cachedLocalIP = $_SERVER['SERVER_ADDR'];
99+
} else {
100+
$os = php_uname('s');
101+
102+
if (stripos($os, 'Linux') !== false) {
103+
$localIP = shell_exec("hostname -I | awk '{print $1}'"); // Linux systems
104+
} elseif (stripos($os, 'Darwin') !== false) {
105+
$localIP = shell_exec("ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | head -n 1"); // macOS
106+
} else {
107+
$localIP = gethostbyname(gethostname()); // Fallback method
108+
}
109+
110+
self::$_cachedLocalIP = trim($localIP ?? '');
111+
}
112+
113+
return self::$_cachedLocalIP;
114+
}
115+
116+
/**
117+
* Used for testing only. Do not use in your code.
118+
*/
119+
public static function setCachedLocalIP(string $ip): void
120+
{
121+
self::$_cachedLocalIP = $ip;
122+
}
88123
}

tests/Unit/LogFileTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Opcodes\LogViewer\LogFile;
44
use Opcodes\LogViewer\Logs\LogType;
5+
use Opcodes\LogViewer\Utils\Utils;
56

67
test('log file can be instantiated with just a path to the file', function () {
78
$path = storage_path('logs/laravel.log');
@@ -23,3 +24,18 @@
2324
->and($type->value)->toBe(LogType::DEFAULT)
2425
->and($type->name())->toBe('Unknown');
2526
});
27+
28+
test('log file identifier is based on server address', function () {
29+
$path = storage_path('logs/laravel.log');
30+
file_put_contents($path, str_repeat('0', 10)); // 10 bytes
31+
// Set the cached local IP to a known value:
32+
Utils::setCachedLocalIP($serverIp = '123.123.123.123');
33+
34+
$logFile = new LogFile($path);
35+
36+
expect($logFile->identifier)->toBe(
37+
Utils::shortMd5($serverIp . ':' . $path) . '-laravel.log'
38+
)->and($logFile->subFolderIdentifier())->toBe(
39+
Utils::shortMd5($serverIp . ':' . $logFile->subFolder)
40+
);
41+
});

tests/Unit/LogFolderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Opcodes\LogViewer\LogFile;
44
use Opcodes\LogViewer\LogFolder;
5+
use Opcodes\LogViewer\Utils\Utils;
56

67
test('LogFolder can get the earliest timestamp of the files it contains', function () {
78
$firstFile = Mockery::mock(new LogFile('folder/test.log'))
@@ -22,3 +23,14 @@
2223

2324
expect($folder->latestTimestamp())->toBe($firstFile->latestTimestamp());
2425
});
26+
27+
test('log folder identifier is based on server address', function () {
28+
// Set the cached local IP to a known value:
29+
Utils::setCachedLocalIP($serverIp = '123.123.123.123');
30+
31+
$folder = new LogFolder('folder', []);
32+
33+
expect($folder->identifier)->toBe(
34+
Utils::shortMd5($serverIp . ':' . $folder->path)
35+
);
36+
});

0 commit comments

Comments
 (0)