You are here

function _clamav_scan_via_exec in ClamAV 6

Same name and namespace in other branches
  1. 7 clamav.inc \_clamav_scan_via_exec()

Scan a single file using a clamav executable.

Parameters

String $filepath:

Return value

int one of:

1 call to _clamav_scan_via_exec()
clamav_scan_file in ./clamav.inc
Scan a single file

File

./clamav.inc, line 114
API and helper functions for the ClamAV module.

Code

function _clamav_scan_via_exec($filepath) {

  // get the path to the executable
  $executable = variable_get('clamav_executable_path', CLAMAV_DEFAULT_PATH);

  // check that the executable is available
  if (!file_exists($executable)) {
    watchdog('clamav', "The clamscan executable could not be found at %path", array(
      '%path' => $executable,
    ), WATCHDOG_ERROR);
    return CLAMAV_SCANRESULT_UNCHECKED;
  }

  // using 2>&1 to grab the full command-line output.
  $cmd = escapeshellcmd($executable) . ' ' . escapeshellarg($filepath) . ' 2>&1';

  // exec:
  // The lines of text output by clamscan are assigned as an array to $output
  // The actual result of clamscan is assigned to $result:
  // 0 = clean
  // 1 = infected
  // x = unchecked
  exec($cmd, $output, $result);

  /**
   * clamscan return values (documented from man clamscan)
   *  0 : No virus found.
   *  1 : Virus(es) found.
   * 40: Unknown option passed.
   * 50: Database initialization error.
   * 52: Not supported file type.
   * 53: Can't open directory.
   * 54: Can't open file. (ofm)
   * 55: Error reading file. (ofm)
   * 56: Can't stat input file / directory.
   * 57: Can't get absolute path name of current working directory.
   * 58: I/O error, please check your file system.
   * 62: Can't initialize logger.
   * 63: Can't create temporary files/directories (check permissions).
   * 64: Can't write to temporary directory (please specify another one).
   * 70: Can't allocate memory (calloc).
   * 71: Can't allocate memory (malloc).
   */
  switch ($result) {
    case 0:
      return CLAMAV_SCANRESULT_CLEAN;
    case 1:

      // pass each line of the exec output through checkplain.
      // The t operator ! is used instead of @, because <br /> tags are being added.
      foreach ($output as $key => $line) {
        $output[$key] = check_plain($line);
      }
      watchdog('clamav', 'Virus detected in uploaded file.  Clamscan reported:<br />!clamscan_output', array(
        '!clamscan_output' => implode('<br />', $output),
      ), WATCHDOG_CRITICAL);
      return CLAMAV_SCANRESULT_INFECTED;
    default:
      $descriptions = array(
        40 => "Unknown option passed.",
        50 => "Database initialization error.",
        52 => "Not supported file type.",
        53 => "Can't open directory.",
        54 => "Can't open file. (ofm)",
        55 => "Error reading file. (ofm)",
        56 => "Can't stat input file / directory.",
        57 => "Can't get absolute path name of current working directory.",
        58 => "I/O error, please check your file system.",
        62 => "Can't initialize logger.",
        63 => "Can't create temporary files/directories (check permissions).",
        64 => "Can't write to temporary directory (please specify another one).",
        70 => "Can't allocate memory (calloc).",
        71 => "Can't allocate memory (malloc).",
      );
      $description = array_key_exists($result, $descriptions) ? $descriptions[$result] : 'unknown error';
      watchdog('clamav', 'Uploaded file could not be scanned.  Clamscan reported: [@error_code] - @error_description', array(
        '@error_code' => $result,
        '@error_description' => $description,
      ), WATCHDOG_WARNING);
      return CLAMAV_SCANRESULT_UNCHECKED;
  }
}