Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
{
"name": "appwrite/php-clamav",
"description": "ClamAV network and pipe client for PHP",
"type": "library",
"keywords": ["php", "clamav", "anti virus", "appwrite"],
"license": "MIT",
"authors": [
{
"name": "Eldad Fux",
"email": "eldad@appwrite.io"
}
],
"autoload": {
"psr-4": {"Appwrite\\ClamAV\\": "src/ClamAV"}
},
"autoload-dev": {
"psr-4": {"Utopia\\Tests\\": "tests/ClamAV"}
},
"require": {
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
},
"minimum-stability": "stable"
"name": "appwrite/php-clamav",
"description": "ClamAV network and pipe client for PHP",
"type": "library",
"keywords": [
"php",
"clamav",
"anti virus",
"appwrite"
],
"license": "MIT",
"authors": [
{
"name": "Eldad Fux",
"email": "eldad@appwrite.io"
}
],
"autoload": {
"psr-4": {
"Appwrite\\ClamAV\\": "src/ClamAV"
}
},
"autoload-dev": {
"psr-4": {
"Appwrite\\ClamAV\\Tests\\": "tests/ClamAV"
}
},
"require": {
"php": ">=7.1",
"ext-sockets": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
},
"minimum-stability": "stable"
}
42 changes: 25 additions & 17 deletions src/ClamAV/ClamAV.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

namespace Appwrite\ClamAV;

use function end;
use function explode;
use function socket_close;
use function socket_recv;
use function socket_send;
use function strlen;
use function trim;

abstract class ClamAV
{
/**
* @var int
*/
const CLAMAV_MAX = 20000;
public const CLAMAV_MAX = 20000;

/**
* @return resource
Expand All @@ -24,9 +32,9 @@ private function sendCommand($command)

$socket = $this->getSocket();

\socket_send($socket, $command, \strlen($command), 0);
\socket_recv($socket, $return, self::CLAMAV_MAX, 0);
\socket_close($socket);
socket_send($socket, $command, strlen($command), 0);
socket_recv($socket, $return, self::CLAMAV_MAX, 0);
socket_close($socket);

return $return;
}
Expand All @@ -38,20 +46,20 @@ private function sendCommand($command)
*
* @return bool
*/
public function ping()
public function ping(): bool
{
$return = $this->sendCommand('PING');
return \trim($return) === 'PONG';
return trim($return) === 'PONG';
}

/**
* Check ClamAV Version
*
* @return string
*/
public function version()
public function version(): string
{
return \trim($this->sendCommand('VERSION'));
return trim($this->sendCommand('VERSION'));
}

/**
Expand Down Expand Up @@ -81,14 +89,14 @@ public function shutdown()
* @param string $file
* @return bool return true if file is OK or false if not
*/
public function fileScan(string $file)
public function fileScan(string $file): bool
{
$out = $this->sendCommand('SCAN ' . $file);
$out = $this->sendCommand('SCAN ' . $file);

$out = \explode(':', $out);
$stats = \end($out);
$out = explode(':', $out);
$stats = end($out);

$result = \trim($stats);
$result = trim($stats);

return ($result === 'OK');
}
Expand All @@ -100,13 +108,13 @@ public function fileScan(string $file)
* @param string $file
* @return array
*/
public function continueScan(string $file)
public function continueScan(string $file): array
{
$return = [];

foreach(\explode("\n", \trim($this->sendCommand('CONTSCAN ' . $file))) as $results ) {
list($file, $stats) = \explode(':', $results);
\array_push($return, [ 'file' => $file, 'stats' => \trim($stats) ]);
foreach (explode("\n", trim($this->sendCommand('CONTSCAN ' . $file))) as $results) {
[$file, $stats] = explode(':', $results);
$return[] = ['file' => $file, 'stats' => trim($stats)];
}

return $return;
Expand Down
18 changes: 11 additions & 7 deletions src/ClamAV/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace Appwrite\ClamAV;

use RuntimeException;
use function socket_connect;
use function socket_create;

class Network extends ClamAV
{
const CLAMAV_HOST = '127.0.0.1';
const CLAMAV_PORT = 3310;
private const CLAMAV_HOST = '127.0.0.1';
private const CLAMAV_PORT = 3310;

/**
* @var string
Expand Down Expand Up @@ -33,15 +37,15 @@ public function __construct(string $host = self::CLAMAV_HOST, int $port = self::

/**
* @return resource
* @throws \Exception
* @throws RuntimeException
*/
protected function getSocket()
{
$socket = @\socket_create(AF_INET, SOCK_STREAM, 0);
$status = @\socket_connect($socket, $this->host, $this->port);
$socket = @socket_create(AF_INET, SOCK_STREAM, 0);
$status = @socket_connect($socket, $this->host, $this->port);

if(!$status) {
throw new \Exception('Unable to connect to ClamAV server');
if (!$status) {
throw new RuntimeException('Unable to connect to ClamAV server');
}
return $socket;
}
Expand Down
9 changes: 6 additions & 3 deletions src/ClamAV/Pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Appwrite\ClamAV;

use function socket_connect;
use function socket_create;

class Pipe extends ClamAV
{
const CLAMAV_HOST = '/var/run/clamav/clamd.ctl';
private const CLAMAV_HOST = '/var/run/clamav/clamd.ctl';

/**
* @var string
Expand All @@ -29,8 +32,8 @@ public function __construct(string $pip = self::CLAMAV_HOST)
*/
protected function getSocket()
{
$socket = \socket_create(AF_UNIX, SOCK_STREAM, 0);
\socket_connect($socket, $this->pip);
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_connect($socket, $this->pip);
return $socket;
}
}
26 changes: 13 additions & 13 deletions tests/ClamAV/ClamAVTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
*/

namespace Utopia\Tests;
namespace Appwrite\ClamAV\Tests;

use Appwrite\ClamAV\Network;
use Appwrite\ClamAV\Pipe;
Expand All @@ -22,12 +22,12 @@ class ClamAVTest extends TestCase
/**
* @var Network
*/
protected $network = null;
protected $network;

/**
* @var Pipe
*/
protected $pipe = null;
protected $pipe;

protected function setUp(): void
{
Expand All @@ -38,27 +38,27 @@ protected function setUp(): void
protected function tearDown(): void
{
$this->network = null;
$this->pipe= null;
$this->pipe = null;
}

public function testVersion()
public function testVersion(): void
{
$this->assertStringStartsWith('ClamAV ', $this->network->version());
self::assertStringStartsWith('ClamAV ', $this->network->version());
}

public function testPing()
public function testPing(): void
{
$this->assertTrue($this->network->ping());
self::assertTrue($this->network->ping());
}

public function testFileScan()
public function testFileScan(): void
{
$this->assertTrue($this->network->fileScan('/home/NoVirus.txt'));
$this->assertFalse($this->network->fileScan('/home/Virus.txt'));
self::assertTrue($this->network->fileScan('/home/NoVirus.txt'));
self::assertFalse($this->network->fileScan('/home/Virus.txt'));
}

public function testReload()
public function testReload(): void
{
$this->assertStringStartsWith('RELOADING', $this->network->reload());
self::assertStringStartsWith('RELOADING', $this->network->reload());
}
}