Skip to content

Commit 479ec27

Browse files
committed
CMS6 compatibility fixes
1 parent 0b36ac1 commit 479ec27

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

src/ClamAV.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,6 @@ protected function getIP(): string
285285
return '';
286286
}
287287

288-
return $request->getIP();
288+
return $request->getIP() ?? '';
289289
}
290290
}

src/Extension/ClamAVExtension.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use SilverStripe\Assets\File;
66
use SilverStripe\Assets\Folder;
7+
use SilverStripe\Control\Controller;
78
use SilverStripe\Core\Extension;
89
use SilverStripe\Core\Injector\Injector;
910
use SilverStripe\Core\Validation\ValidationResult;
@@ -151,9 +152,17 @@ public function getFullPath(bool $stripQueryParams = false): ?string
151152
return null;
152153
}
153154

155+
// getSourceURL() calls FlysystemAssetStore::grant() for protected files,
156+
// which requires an active controller/request. During dev/build with no
157+
// database no controller exists, so fall back to the stored filename
158+
// which is sufficient for the scan log record.
159+
if (!Controller::curr()) {
160+
return $owner->getFilename() ?: null;
161+
}
162+
154163
$sourceUrl = $this->owner->File->getSourceURL() ?? '';
155164
if ($stripQueryParams) {
156-
return strtok($sourceUrl, '?');
165+
return strtok($sourceUrl, '?') ?: null;
157166
}
158167

159168
return $sourceUrl;

src/Model/ClamAVScan.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ public function onBeforeWrite(): void
223223
$member = Security::getCurrentUser();
224224
if ($member) {
225225
$this->MemberID = $member->ID;
226+
} else {
227+
$this->MemberID = 0;
226228
}
227-
$this->MemberID = 0;
228229
}
229230
}
230231

@@ -447,7 +448,7 @@ public function getStateMessage(): DBHTMLText
447448
$colour = '#C00';
448449
$text = '';
449450

450-
$action = $this->State;
451+
$action = $this->getState();
451452
$state_messages = $this->config()->state_messages;
452453
if (!isset($state_messages[$action])) {
453454
$action = self::STATE_INVALID;
@@ -492,16 +493,17 @@ public function getUserIdentifier(): ?string
492493
{
493494
if ($this->MemberID) {
494495
$member = $this->Member();
495-
496-
return $member->Email . ' #' . $member->ID . ' (' . $this->IPAddress . ')';
496+
if ($member && $member->exists()) {
497+
return $member->Email . ' #' . $member->ID . ' (' . $this->IPAddress . ')';
498+
}
497499
}
498500

499501
return $this->IPAddress;
500502
}
501503

502504
public function getRawDataSummary(): ?string
503505
{
504-
$rawData = $this->RawData;
506+
$rawData = $this->getRawData();
505507

506508
return ($rawData && isset($rawData['status'])) ? $rawData['status'] : '';
507509
}

src/Tasks/Traits/ScanTrait.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,27 @@ protected function scanList(SS_List $list): bool
7171
}
7272
$logRecord = $file->scanForVirus();
7373

74-
// scans by a job/task will have an IPAddress of 127.0.0.1
75-
$originalScan = $file->ClamAVScans()->sort('Created DESC')->first();
76-
if (isset($originalScan, $originalScan->IPAddress)) {
77-
// replace 127.0.0.1 with original IPAddress
78-
$logRecord->IPAddress = $originalScan->IPAddress;
74+
// Null guard must precede any property access on $logRecord.
75+
if (!$logRecord) {
76+
$this->log('Skipping ' . $file->ClassName . ' #' . $file->ID . '. File doesn\'t exist.');
77+
78+
continue;
7979
}
8080

81-
if ($logRecord === ClamAV::OFFLINE) {
81+
// scanFileForVirus() catches socket exceptions and returns an unscanned
82+
// ClamAVScan record (IsScanned = false) when the daemon is unreachable,
83+
// rather than throwing. An unscanned record here means the daemon is down.
84+
if (!$logRecord->IsScanned) {
8285
$this->log('ClamAV daemon is offline.', 'error');
8386

8487
return false;
8588
}
86-
if (!$logRecord) {
87-
$this->log('Skipping ' . $file->ClassName . ' #' . $file->ID . '. File doesn\'t exist.');
8889

89-
continue;
90+
// scans by a job/task will have an IPAddress of 127.0.0.1
91+
$originalScan = $file->ClamAVScans()->sort('Created DESC')->first();
92+
if (isset($originalScan, $originalScan->IPAddress)) {
93+
// replace 127.0.0.1 with original IPAddress
94+
$logRecord->IPAddress = $originalScan->IPAddress;
9095
}
9196
if ($logRecord->IsInfected) {
9297
$this->log($file->ClassName . ' #' . $file->ID . ' has a virus.', 'error');

0 commit comments

Comments
 (0)