DaemonTCPIP.php in ClamAV 8
File
src/Scanner/DaemonTCPIP.php
View source
<?php
namespace Drupal\clamav\Scanner;
use Drupal\file\FileInterface;
use Drupal\clamav\ScannerInterface;
use Drupal\clamav\Scanner;
use Drupal\clamav\Config;
class DaemonTCPIP implements ScannerInterface {
protected $_file;
protected $_hostname;
protected $_port;
protected $_virus_name = '';
public function __construct(Config $config) {
$this->_hostname = $config
->get('mode_daemon_tcpip.hostname');
$this->_port = $config
->get('mode_daemon_tcpip.port');
}
public function scan(FileInterface $file) {
$scanner_handler = @fsockopen($this->_hostname, $this->_port);
if (!$scanner_handler) {
\Drupal::logger('Clam AV')
->warning('Unable to connect to ClamAV TCP/IP daemon on @hostname:@port', array(
'@hostname' => $this->_hostname,
'@port' => $this->_port,
));
return Scanner::FILE_IS_UNCHECKED;
}
$bytes = $file
->getSize();
fwrite($scanner_handler, "zINSTREAM\0");
fwrite($scanner_handler, pack("N", $bytes));
$file_handler = fopen($file
->getFileUri(), 'r');
stream_copy_to_stream($file_handler, $scanner_handler);
fwrite($scanner_handler, pack("N", 0));
$response = trim(fgets($scanner_handler));
fclose($scanner_handler);
fclose($file_handler);
if (preg_match('/^stream: OK$/', $response)) {
$result = Scanner::FILE_IS_CLEAN;
}
elseif (preg_match('/^stream: (.*) FOUND$/', $response, $matches)) {
$this->_virus_name = $matches[1];
$result = Scanner::FILE_IS_INFECTED;
}
else {
preg_match('/^stream: (.*) ERROR$/', $response, $matches);
$result = Scanner::FILE_IS_UNCHECKED;
}
return $result;
}
public function virus_name() {
return $this->_virus_name;
}
public function version() {
$handler = @fsockopen($this->_hostname, $this->_port);
if (!$handler) {
\Drupal::logger('Clam AV')
->warning('Unable to connect to ClamAV TCP/IP daemon on @hostname:@port', array(
'@hostname' => $this->_hostname,
'@port' => $this->_port,
));
return NULL;
}
fwrite($handler, "VERSION\n");
$content = fgets($handler);
fclose($handler);
return $content;
}
}