From 10cc7009e038c1ee3145e8bbd9a8be601b4ef67d Mon Sep 17 00:00:00 2001 From: matthew Date: Fri, 7 Sep 2018 20:10:46 -0400 Subject: [PATCH] Socket Tests and updated README Updates ClamavScan to ping ClamAV socket during each attempted send. Updated README file --- README.md | 4 ++-- src/Adapter/ClamavScan.php | 47 ++++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e537f0f..60fc0c1 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # php-clamav # -Package to check files against ClamAV. You can pass a file via UNIX and TCP sockets. It ClamAV is installed locally you can also use clamscan/clamdscan. +PHP library to check files against ClamAV. You can pass a file via UNIX and TCP sockets. If ClamAV is installed locally you can also use clamscan/clamdscan. ## Install ## -Assumes you already have ClamAV installed locally or on a secured remote server. +You should have ClamAV installed locally or on a secured remote server. ### Composer ### diff --git a/src/Adapter/ClamavScan.php b/src/Adapter/ClamavScan.php index a95af35..9ae2d2a 100644 --- a/src/Adapter/ClamavScan.php +++ b/src/Adapter/ClamavScan.php @@ -35,6 +35,13 @@ public function scan($fileHandle, $fileSize, $options) { $socket = new ClamavSocket(); $openSocket = $socket->openSocket($options); + /* + * Check if clamav is available if not return message + */ + $checkSocket = $socket->checkSocket($options); + if ($checkSocket['message'] != "ClamAV is Alive!") { + return $checkSocket; + } $sendResponse['instream'] = $socket->send($openSocket, $zInstream); @@ -45,20 +52,32 @@ public function scan($fileHandle, $fileSize, $options) { fseek($fileHandle, $chunkDataSent); $chunk = fread($fileHandle, $options['clamavChunkSize']); $chunkLength = pack("N", strlen($chunk)); - $chunkLengthResponse = $socket->send($openSocket, $chunkLength); - $chunkDataResponse = $socket->send($openSocket, $chunk); - $chunkDataSent += $chunkDataResponse['written']; + /* + * Check if clamav is available if not return message + */ + if ($checkSocket['message'] != "ClamAV is Alive!") { + return $checkSocket; + $chunkLengthResponse = $socket->send($openSocket, $chunkLength); + $chunkDataResponse = $socket->send($openSocket, $chunk); + $chunkDataSent += $chunkDataResponse['written']; + } + /* + * Currently do not need to send zero string to Clamav with this code. + * Leaving it here for the time being for update to how a file is sent to clamvav host socket. + */ + $endInstream = pack("N", strlen("")) . ""; + /* + * Check if clamav is available if not return message + */ + $checkSocket = $socket->checkSocket($options); + if ($checkSocket['message'] != "ClamAV is Alive!") { + return $checkSocket; + } + $response = $socket->send($openSocket, $endInstream, 1); + $socket->closeSocket($openSocket); } - /* - * Currently do not need to send zero string to Clamav with this code. - * Leaving it here for the time being for update to how a file is sent to clamvav host socket. - */ - $endInstream = pack("N", strlen("")) . ""; - $response = $socket->send($openSocket, $endInstream, 1); - $socket->closeSocket($openSocket); - } - return $response; + return $response; - } + } -} \ No newline at end of file + } \ No newline at end of file