diff --git a/composer.json b/composer.json index ecc8663..35b3bb8 100755 --- a/composer.json +++ b/composer.json @@ -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" } diff --git a/src/ClamAV/ClamAV.php b/src/ClamAV/ClamAV.php index 1402fff..f7f6885 100644 --- a/src/ClamAV/ClamAV.php +++ b/src/ClamAV/ClamAV.php @@ -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 @@ -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; } @@ -38,10 +46,10 @@ 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'; } /** @@ -49,9 +57,9 @@ public function ping() * * @return string */ - public function version() + public function version(): string { - return \trim($this->sendCommand('VERSION')); + return trim($this->sendCommand('VERSION')); } /** @@ -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'); } @@ -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; diff --git a/src/ClamAV/Network.php b/src/ClamAV/Network.php index c55cc32..438f83d 100644 --- a/src/ClamAV/Network.php +++ b/src/ClamAV/Network.php @@ -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 @@ -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; } diff --git a/src/ClamAV/Pipe.php b/src/ClamAV/Pipe.php index 80c25f4..e596bc0 100644 --- a/src/ClamAV/Pipe.php +++ b/src/ClamAV/Pipe.php @@ -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 @@ -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; } } diff --git a/tests/ClamAV/ClamAVTest.php b/tests/ClamAV/ClamAVTest.php index 385267d..f980ca9 100755 --- a/tests/ClamAV/ClamAVTest.php +++ b/tests/ClamAV/ClamAVTest.php @@ -11,7 +11,7 @@ * @license The MIT License (MIT) */ -namespace Utopia\Tests; +namespace Appwrite\ClamAV\Tests; use Appwrite\ClamAV\Network; use Appwrite\ClamAV\Pipe; @@ -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 { @@ -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()); } }