You are here

function clamav_file_validate in ClamAV 8

Same name and namespace in other branches
  1. 7 clamav.module \clamav_file_validate()
  2. 2.x clamav.module \clamav_file_validate()

Implements hook_file_validate().

File

./clamav.module, line 24
Controls behaviour of anti-virus integration with ClamAV.

Code

function clamav_file_validate(Drupal\file\FileInterface $file) {
  $errors = array();

  // This hook can be called on actions that do not justify anti-virus scanning
  // (such as removing an existing file).
  // The entity-action attaches a property "clamav_attemptScan" for actions
  // where the file should be scanned.
  if (empty($file->clamav_attemptScan)) {
    return $errors;
  }
  $scanner = \Drupal::service('clamav');
  if ($scanner
    ->isEnabled() && $scanner
    ->isScannable($file)) {
    if ($scanner
      ->isVerboseModeEnabled()) {
      \Drupal::logger('Clam AV')
        ->debug('File %filename is scannable.', array(
        '%filename' => $file
          ->getFilename(),
      ));
    }
    $result = $scanner
      ->scan($file);
    switch ($result) {

      // File is infected.
      case Scanner::FILE_IS_INFECTED:
        $errors[] = t('A virus has been detected in the file. The file will be deleted.');
        break;

      // File couldn't be scanned (perhaps the ClamAV service is unavailable).
      case Scanner::FILE_IS_UNCHECKED:

        // Check whether unscanned files are permitted
        if (!$scanner
          ->allowUncheckedFiles()) {
          $errors[] = t('The anti-virus scanner could not check the file, so the file cannot be uploaded. Contact the site administrator if this problem persists.');
        }
        break;
    }
  }
  elseif ($scanner
    ->isVerboseModeEnabled()) {
    $message = 'Uploaded file %filename was not checked, and was uploaded without checking.';
    $replacements = array(
      '%filename' => $file
        ->getFilename(),
    );
    \Drupal::logger('Clam AV')
      ->info($message, $replacements);
  }
  return $errors;
}