You are here

public function Scanner::scan in ClamAV 8

Same name and namespace in other branches
  1. 2.x src/Scanner.php \Drupal\clamav\Scanner::scan()

Scan a file for viruses.

Parameters

Drupal\file\FileInterface $file: The file to scan for viruses.

Return value

int One of the following class constants:

  • CLAMAV_SCANRESULT_UNCHECKED The file was not scanned. The ClamAV service may be unavailable.
  • CLAMAV_SCANRESULT_CLEAN The file was scanned, and no infection was found.
  • CLAMAV_SCANRESULT_INFECTED The file was scanned, and found to be infected with a virus.

File

src/Scanner.php, line 146

Class

Scanner
Service class for the ClamAV scanner instance.

Namespace

Drupal\clamav

Code

public function scan(FileInterface $file) {

  // Empty files are never infected.
  if ($file
    ->getSize() === 0) {
    return self::FILE_IS_CLEAN;
  }
  $result = $this->scanner
    ->scan($file);

  // Prepare to log results.
  $verbose_mode = $this->config
    ->verbosity();
  $replacements = array(
    '%filename' => $file
      ->getFileUri(),
    '%virusname' => $this->scanner
      ->virus_name(),
  );
  switch ($result) {

    // Log every infected file.
    case self::FILE_IS_INFECTED:
      $message = 'Virus %virusname detected in uploaded file %filename.';
      \Drupal::logger('Clam AV')
        ->error($message, $replacements);
      break;

    // Log clean files if verbose mode is enabled.
    case self::FILE_IS_CLEAN:
      if ($verbose_mode) {
        $message = 'Uploaded file %filename checked and found clean.';
        \Drupal::logger('Clam AV')
          ->info($message, $replacements);
      }
      break;

    // Log unchecked files if they are accepted, or verbose mode is enabled.
    case self::FILE_IS_UNCHECKED:
      if ($this->config
        ->outage_action() == Config::OUTAGE_ALLOW_UNCHECKED) {
        $message = 'Uploaded file %filename could not be checked, and was uploaded without checking.';
        \Drupal::logger('Clam AV')
          ->notice($message, $replacements);
      }
      elseif ($verbose_mode) {
        $message = 'Uploaded file %filename could not be checked, and was deleted.';
        \Drupal::logger('Clam AV')
          ->info($message, $replacements);
      }
      break;
  }
  return $result;
}