Skip to content

Commit

Permalink
Add support for CMS 5
Browse files Browse the repository at this point in the history
  • Loading branch information
satrun77 committed Jan 23, 2024
1 parent f1dc321 commit c9cd36f
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 86 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Module CI

on:
push:
pull_request:

jobs:
ci:
name: CI
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
with:
endtoend: false
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
.idea
.idea
.phpunit.result.cache
composer.lock
public/
vendor/
31 changes: 0 additions & 31 deletions .travis.yml

This file was deleted.

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ nightly cron that scans the files or if you have queuedjobs installed, it will a
# Composer Install

```
composer require symbiote/silverstripe-steamedclams:~2.0
composer require symbiote/silverstripe-steamedclams
```

# Screenshots
Expand Down Expand Up @@ -137,9 +137,10 @@ ClamAVEmulator::config()->mode = ClamAVEmulator::MODE_OFFLINE;
```

# Supports
- Silverstripe 4.0 and up
- Silverstripe 5.0 and up
- [Versioned Files](https://github.com/symbiote/silverstripe-versionedfiles)
- [CDN Content](https://github.com/symbiote/silverstripe-cdncontent)
- For Silverstripe 4.x use 3.0
- For Silverstripe 3.2 and up (3.1 *should* work, create an issue if you determine otherwise) use 1.0

# Credits
Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
}
],
"require": {
"silverstripe/framework": "^4"
"silverstripe/framework": "^5",
"silverstripe/admin": "^2",
"silverstripe/reports": "^5",
"silverstripe/siteconfig": "^5"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"suggest": {
"silverstripe/queuedjobs": "For allowing ClamAV 'missed files' scan to be run from a queued job. Otherwise you can run the tasks manually or via cronjob."
},
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
"dev-master": "4.0.x-dev"
},
"expose": [
"client/css",
Expand Down
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
12 changes: 6 additions & 6 deletions src/Admin/ClamAVAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ClamAVAdmin extends ModelAdmin
/**
* @var string
*/
private static $menu_icon = 'vendor/symbiote/silverstripe-steamedclams/client/images/clamav_icon.png';
private static $menu_icon = 'symbiote/silverstripe-steamedclams:client/images/clamav_icon.png';

/**
* @var array
Expand Down Expand Up @@ -117,7 +117,7 @@ public function getEditForm($id = null, $fields = null)
$versionField = LiteralField::create('ClamAV_Version', $version);
$versionField->setRightTitle($reason);
$versionField->dontEscape = true;
$fields->insertBefore($versionField, $insertBeforeFieldName);
$fields->insertBefore($insertBeforeFieldName, $versionField);

// Files to scan with install task
$listCount = 0;
Expand All @@ -129,12 +129,12 @@ public function getEditForm($id = null, $fields = null)

if ($listCount > 0) {
$fields->insertBefore(
$insertBeforeFieldName,
ReadonlyField::create(
'ClamAV_InitialScan',
'Files to scan with install task',
$listCount . ' '
),
$insertBeforeFieldName
)
);
}

Expand All @@ -145,9 +145,9 @@ public function getEditForm($id = null, $fields = null)
$listCount = $list->count();
}
$fields->insertBefore(
$insertBeforeFieldName,
ReadonlyField::create('ClamAV_NeedScan', 'Files that failed to scan', $listCount . ' ')
->setRightTitle('Due to ClamAV daemon being inaccessible/offline.'),
$insertBeforeFieldName
->setRightTitle('Due to ClamAV daemon being inaccessible/offline.')
);
});

Expand Down
12 changes: 12 additions & 0 deletions src/ClamAVEmulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Symbiote\SteamedClams;

use LogicException;
use SilverStripe\Assets\File;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataObject;

/**
* For emulating/faking ClamAV results
Expand Down Expand Up @@ -117,4 +119,14 @@ protected function modeInvalid()
. '". Use constants provided in ' . __CLASS__ . ' class.'
);
}

public function scanFileRecordForVirus(File $file)
{
$record = $this->scanFileForVirus('fake/file.txt');
if ($record && $record instanceof DataObject) {
$record->FileID = $file->ID;
}
return $record;
}

}
23 changes: 16 additions & 7 deletions src/Extension/ClamAVExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use SilverStripe\Assets\File;
use SilverStripe\Assets\Flysystem\ProtectedAssetAdapter;
use SilverStripe\Assets\Folder;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationResult;
use Symbiote\SteamedClams\ClamAV;
use Symbiote\SteamedClams\Model\ClamAVScan;
use SilverStripe\Core\Config\Config;
use Silverstripe\SiteConfig\SiteConfig;

/**
Expand Down Expand Up @@ -163,15 +163,24 @@ public function getFullPath()
return null;
}

$fileMetaData = $this->owner->File->getMetadata();

// For publicly accessible file, return the path from URL of the file
if ($this->owner->isPublished()) {
return PUBLIC_PATH . $this->owner->File->getURL();
} else {
return ASSETS_PATH . '/' .
Config::inst()->get(ProtectedAssetAdapter::class, 'secure_folder')
. '/' . $fileMetaData['path'];
}

// For protected file, construct the expected path of the file based on
// - Asset folder name
// - Name of the secure folder
// - URL of the file
$assetDir = DIRECTORY_SEPARATOR . ASSETS_DIR . DIRECTORY_SEPARATOR;
$assetDirLength = strlen($assetDir);
$sourceUrl = $this->owner->File->getSourceURL();

return DIRECTORY_SEPARATOR . File::join_paths(
ASSETS_PATH,
Config::inst()->get(ProtectedAssetAdapter::class, 'secure_folder'),
str_starts_with($sourceUrl, $assetDir) ? substr($sourceUrl, $assetDirLength) : $sourceUrl
);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Model/ClamAVScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public function getRawData()
{
$value = $this->getField('RawData');
if (is_string($value)) {
$value = Convert::json2array($value);
$value = json_decode($value, true);
}

return $value;
Expand All @@ -561,11 +561,12 @@ public function getRawData()
* @param array $value
*
* @return null
* @throws \JsonException
*/
public function setRawData($value)
{
if (is_array($value)) {
$value = Convert::array2json($value);
$value = json_encode($value, JSON_THROW_ON_ERROR);
}
$this->setField('RawData', $value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/ClamAVBaseTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected function log($messageOrDataObject, $type = '', Exception $exception =
* @param $errline
* @param $errcontext
*/
public function log_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
public function log_error_handler(int $errno, string $errstr, string $errfile = null, int $errline = null, array $errcontext = null): void
{
DB::alteration_message($errstr, 'error');

Expand Down
12 changes: 8 additions & 4 deletions tests/ClamAVCMSTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Symbiote\SteamedClams;
namespace Symbiote\SteamedClams\Tests;

use SilverStripe\Dev\FunctionalTest;
use Symbiote\SteamedClams\Admin\ClamAVAdmin;
use SilverStripe\Reports\Report;
use Symbiote\SteamedClams\Reports\ClamAVScanReport;

/**
/**
* Check various CMS areas of the ClamAV module and ensure it's not
* breaking via unset variables, typos, etc.
*/
Expand All @@ -20,19 +20,21 @@ class ClamAVCMSTest extends FunctionalTest
/**
*
*/
public function testModelAdmin()
public function testModelAdmin(): void
{
$this->logInAs('admin');

// Test ModelAdmin listing
$controller = singleton(ClamAVAdmin::class);
$response = $this->get($controller->Link());

$this->assertEquals(200, $response->getStatusCode());
}

/**
*
*/
public function testClamAVReport()
public function testClamAVReport(): void
{
if (!class_exists(Report::class)) {
return;
Expand All @@ -43,5 +45,7 @@ public function testClamAVReport()
// Test Report page
$controller = singleton(ClamAVScanReport::class);
$response = $this->get($controller->getLink());

$this->assertEquals(200, $response->getStatusCode());
}
}
Loading

0 comments on commit c9cd36f

Please sign in to comment.