AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 7.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • ACL

    • Introduction
    • How to install
    • Setup
    • Permissions
    • Roles
    • Users
    • Cached Permissions
  • Antivirus

    • Introduction
    • How to install
    • Setup
    • How to use
    • Scanners

      • Introduction
      • ClamAV
      • Null
      • Custom
    • Events
    • PSR
  • Audit

    • Audit
    • How to install
    • Setup
    • Recording
    • Events
  • Auth

    • Introduction
    • How to install
    • Fortify

      • Prerequisites
      • Actions

        • Rehash Password
  • Circuits

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • Collections

    • Collections
    • How to install
    • Summation

      • Summation Collection
      • Items Processor
  • Config

    • Configuration Loader
    • How to install
    • Setup
    • Load Configuration Files
    • Custom File Parsers
  • Console

    • Command and Schedule Registration
    • How to install
    • Setup
    • Commands
    • Schedules
  • Container

    • IoC Service Container
    • How to install
    • Container
    • List Resolver
  • Core

    • Athenaeum Core Application
    • How to install
    • Setup
    • Usage

      • Configuration
      • Service Providers
      • Service Container
      • Events
      • Caching
      • Logging
      • Console
      • Task Scheduling
      • Exception Handling
      • Extending Core Application
      • Testing
  • Database

    • Introduction
    • How to install
    • Models

      • Instantiatable
      • Sluggable
    • Query

      • Criteria (Query Filter)
  • Dto

    • Data Transfer Object (DTO)
    • How to install
    • Create Interface
    • Implement DTO
    • How to use
    • Populate
    • Export
    • Json
    • Serialization
    • Nested DTOs
    • Array DTO
  • ETags

    • Introduction
    • How to install
    • Setup
    • ETags usage

      • How to use
      • Generators

        • Default Generator
        • Custom Generator
      • Eloquent Models
    • Http Request Preconditions

      • Introduction
      • Resource Context
      • Preconditions
      • Actions
      • RFC 9110

        • If-Match
        • If-Unmodified-Since
        • If-None-Match
        • If-Modified-Since
        • If-Range
      • Extensions

        • Introduction
        • Range
      • Range Validator
      • Download Stream
    • Macros
  • Events

    • Register Listeners and Subscribers
    • How to install
    • Setup
    • Listeners
    • Subscribers
  • Filters

    • Search Filter Utilities
    • Prerequisites
    • How to install
    • Setup
    • Processor
    • Filters Builder
    • Predefined Resources

      • Search Processor
      • Sorting Processor
      • Constraints Processor
      • Matching Processor
    • Tip: Create a base builder
  • Flysystem

    • Introduction
    • Database Adapter

      • Introduction
      • How to install
      • Setup
      • Data Deduplication
      • MIME-Type Detection
  • Http

    • Api

      • Http API
      • How to install
      • Setup
      • Resources

        • Introduction
        • Timestamps
        • Self-Link
        • Relations
        • Caching
        • Registrar
      • Requests

        • Introduction
        • List Resources
        • List Deleted
        • Show Resource
        • Create Resource
        • Update Resource
        • Delete Resource
        • List Related
        • Process Multiple Resources
        • Helpers
      • Middleware

        • Introduction
        • Request Must Be Json
        • Capture Fields To Select
        • Remove Response Payload
    • Clients

      • Http Clients
      • How to install
      • Setup
      • Basic Usage
      • Available Methods

        • Fluent Api
        • Protocol Version
        • Base Uri
        • Http Method and Uri
        • Headers
        • Accept & Content-Type
        • Authentication
        • Http Query
        • Payload Format
        • Payload
        • Attachments
        • Cookies
        • Response Expectations
        • Middleware
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Debugging
        • Logging
        • Driver Options
        • Driver
      • Http Query Builder

        • Introduction
        • Select
        • Where
        • Dates
        • Include
        • Pagination
        • Sorting
        • Raw Expressions
        • Custom Grammar
    • Cookies

      • Http Cookies
      • How to install
      • Usage
    • Messages

      • Http Messages
      • How to install
      • Serializers
  • Maintenance

    • Modes

      • Maintenance Modes
      • How to install
      • Setup
      • Basic Usage
      • Available Drivers
  • Mime Types

    • MIME-Types
    • How to install
    • Setup
    • Usage
    • Drivers

      • Available Drivers
      • File Info
  • Properties

    • Properties Overload
    • How to install
    • Usage
    • Naming Convention
    • Properties Visibility
  • Redmine

    • Redmine Api Client
    • How to install
    • Setup
    • General Usage

      • Supported Operations
      • Fetch list of resources
      • Find
      • Fetch
      • Create new record
      • Update existing record
      • Delete existing record
      • Relations
    • Available Resources

      • Predefined Resources
      • Attachments
      • Enumerations
      • Issue Relations
      • Users
      • User Groups
      • Roles
      • Project Memberships
      • Versions (Milestones)
      • Issue Categories
      • Trackers
  • Service

    • Service Registrar
    • How to install
    • How to use
  • Streams

    • Streams
    • How to install
    • Setup
    • How to use

      • Introduction
      • Open and Close
      • Raw Resource
      • Seeking
      • Reading
      • Writing
      • Size
      • Truncate
      • Sync
      • Flush
      • Hash
      • MIME-Type
      • Filename
      • Output
      • Locking
      • Transactions
      • Meta
      • Misc
  • Support

    • Introduction
    • How to install
    • Laravel Aware-of Helpers

      • How to use
      • Enforce Via Interface
      • Custom Default
      • Pros and Cons
      • Available Helpers
    • Aware-of Properties

      • Generator
      • Available Aware-of Helpers
    • Live Templates
  • Testing

    • Introduction
    • How to install
    • Test Cases
    • Testing Aware-of Helpers
  • Translation

    • Introduction
    • How to install
    • Exporters

      • Introduction
      • Setup
      • How to use
      • Drivers

        • Introduction
        • Array
        • Lang.js (Array)
        • Lang.js (JSON)
        • Cache
  • Utils

    • Introduction
    • How to install
    • Array
    • Duration
    • Json
    • Math
    • Memory
    • Method Helper
    • Invoker
    • Populatable
    • String
    • Version
  • Validation

    • Introduction
    • How to install
    • Setup
    • Rules

      • Alpha-Dash-Dot
      • Date Format
      • Semantic Version
You are viewing documentation for an outdated version. It is no longer supported!

How to use

  • Manager
  • Scanner
    • Scan a File
    • Supported Types
    • Result & Status
    • Is File Clean
  • Facade
  • Validation Rule

Manager

A Manager is responsible for obtaining an antivirus scanner instance. To obtain the Manager instance, you can use AntivirusManagerTrait.

use Aedart\Antivirus\Traits\AntivirusManagerTrait;

class FilesController
{
    use AntivirusManagerTrait;
    
    public function index()
    {
        $manager = $this->getAntivirusManager();
        
        // ...remaining not shown...
    }
}

Scanner

Before you can scan a file, you must first obtain a scanner instance from the Manager. This can be achieved via the profile() method.

$scanner = $manager->profile(); // Default profile

To obtain a scanner for a specific profile, specify the profile's name.

$scanner = $manager->profile('my-scanner-profile');

Scan a File

The scan() method is used for scanning a file for infections, e.g. viruses, malware or other harmful code. It returns a ScanResult instance, which contains a status and few details about what was scanned.

$result = $scanner->scan('contacts.txt');

print_r($result->toArray());

The output of the above shown example can be similar to this:

Array
(
    [status] => Infected: Win.Test.EICAR_HDB-1
    [filename] => contacts.txt
    [filepath] => /tmp/phpm7zz6s
    [filesize] => 68
    [datetime] => 2023-03-01T08:15:18.753Z
    [user] => null 
    [details] => Array
        (
            [profile] => default
            [clamav_session_id] => 1
        )
)

Supported Types

The scan() method accepts the following types as it's file argument:

  • string path to file.
  • SplFileInfo uploaded file (e.g. Laravel's UploadedFile instance).
  • FileStream file stream.
  • UploadedFileInterface PSR-7 uploaded file instance.
  • StreamInterface PSR-7 stream instance.

Using PSR-7 Components

There is a considerable performance cost, when using scan() in combination with PSR components. Please read the PSR Uploaded Files & Streams chapter, before using!

Result & Status

The ScanResult instance can be used to determine if the scanned file was clean. A scan is ONLY considered okay when the following conditions are meet:

  • Antivirus has scanned the file.
  • No infections were found (virus, malware,...etc).
  • No scanning failure occurred, e.g. timeout, could not read file, filetype unsupported... etc.
// Determine if file is clean (ok)
if ($result->isOk()) {
    // ... do something when file is clean ...
}

// Or, the Opposite of isOk
if ($result->hasFailed()) {
    // ... take action, e.g. abort the request...
}

You can also obtain the result's Status, of the scanned file. The status is always specific to the scanner driver and may provide you with more specific details about why a result isn't okay.

$status = $result->status();

// General for all status instances
if ( ! $status->isOk() && $status->hasReason()) {
    echo $status->reason();
    
    // ...etc
}

// ------------------------------------------------------------ //
// Driver specific

// E.g. for ClamAV scanner
if($status->hasInfection() || $status->hasError()) {
    // ...
}

Is File Clean

As an alternative to scanning a file and manually checking the scan result, you can use the isClean() method. It returns true if the scanned file is clean.

if ($scanner->isClean('contacts.txt')) {
    // ...
}

// Equivalent to the above...
if ($scanner->scan('contacts.txt')->isOk()) {
    // ...
}

Facade

An alternative way of performing file scans, is by using the Antivirus Facade. It allows you to perform a scan directly.

use Aedart\Antivirus\Facades\Antivirus;

$result = Antivirus::scan($file); // Scan using default profile

// Or, check if file is clean...
if (Antivirus::isClean($file)) {
    // ...
}

You can also use the facade to obtain a scanner for a specific profile.

$scanner = Antivirus::profile('my-scanner-profile');

Validation Rule

This package also comes with a custom validation rule, which can prevent upload of infected files.

use Aedart\Antivirus\Validation\Rules\InfectionFreeFile;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

Route::post('/files', function (Request $request) {
    $request->validate([
        'file' => [
            'required',
            'file',
            new InfectionFreeFile()
        ]
    ]);

    $file = $request->file('file');

    // ... do something with uploaded file...
});

Unless otherwise specified, the InfectionFreeFile rule will use the default scanner profile. To use a different profile, specify the profile name as the first argument.

$rule = new InfectionFreeFile('my-scanner-profile');
Edit page
Last Updated: 18/03/2024, 10:45
Contributors: alin
Prev
Setup