From 63068f6b1d39d9f8b63e2e35d0ea3a131b8b83ba Mon Sep 17 00:00:00 2001 From: eldadfux Date: Sat, 11 May 2019 18:30:53 +0300 Subject: [PATCH] Exported ClamAV client to 3rd party package --- app/controllers/storage.php | 8 +-- composer.json | 3 +- composer.lock | 47 ++++++++++++++- docker-compose.yml | 2 +- docker-stack.yml | 2 +- src/ClamAV/ClamAV.php | 112 ------------------------------------ src/ClamAV/Network.php | 48 ---------------- src/ClamAV/Pipe.php | 30 ---------- 8 files changed, 54 insertions(+), 198 deletions(-) delete mode 100644 src/ClamAV/ClamAV.php delete mode 100644 src/ClamAV/Network.php delete mode 100644 src/ClamAV/Pipe.php diff --git a/app/controllers/storage.php b/app/controllers/storage.php index 1ac84fdff..9ea7f3a5c 100644 --- a/app/controllers/storage.php +++ b/app/controllers/storage.php @@ -10,6 +10,7 @@ use Utopia\Validator\Text; use Utopia\Validator\HexColor; use Utopia\Cache\Cache; use Utopia\Cache\Adapter\Filesystem; +use Appwrite\ClamAV\Network; use Database\Database; use Database\Validator\UID; use Storage\Storage; @@ -18,7 +19,6 @@ use Storage\Validators\File; use Storage\Validators\FileSize; use Storage\Compression\Algorithms\GZIP; use Resize\Resize; -use LibreOffice\LibreOffice; use OpenSSL\OpenSSL; Storage::addDevice('local', new Local('app-' . $project->getUid())); @@ -529,7 +529,7 @@ $utopia->post('/v1/storage/files') } } - $antiVirus = new \ClamAV\Network('clamav', 3310); + $antiVirus = new Network('clamav', 3310); /** * Models @@ -680,7 +680,7 @@ $utopia->get('/v1/storage/files/:fileId/scan') $source = $compressor->decompress($source); - $antiVirus = new \ClamAV\Network('clamav', 3310); + $antiVirus = new Network('clamav', 3310); //var_dump($antiVirus->ping()); //var_dump($antiVirus->version()); @@ -701,7 +701,7 @@ $utopia->get('/v1/storage/scan') { $device = Storage::getDevice('local'); - $antiVirus = new \ClamAV\Network('clamav', 3310); + $antiVirus = new Network('clamav', 3310); //var_dump($antiVirus->ping()); //var_dump($antiVirus->version()); //var_dump($antiVirus->fileScan('/storage/uploads/app-1/5/9/f/e/59fecaed49645.pdf')); diff --git a/composer.json b/composer.json index e933b9e75..98f17b1ad 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,6 @@ "psr-4": { "Audit\\": "src/Audit", "Auth\\": "src/Auth", - "ClamAV\\": "src/ClamAV", "Database\\": "src/Database", "Event\\": "src/Event", "OpenSSL\\": "src/OpenSSL", @@ -38,6 +37,8 @@ "ext-zlib": "*", "ext-sockets": "*", + "appwrite/php-clamav": "master", + "utopia-php/framework": "master", "utopia-php/abuse": "master", "utopia-php/cache": "master", diff --git a/composer.lock b/composer.lock index f41e31aa6..76528184c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,53 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "57d614ec24a3ebc4cee0e6fd1f4cebfb", + "content-hash": "c1d8468f36e74c0212272b585e01ffaf", "packages": [ + { + "name": "appwrite/php-clamav", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/appwrite/php-clamav.git", + "reference": "9298a2d490e064f0c72d86ecc9f4792a49f4288f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/appwrite/php-clamav/zipball/9298a2d490e064f0c72d86ecc9f4792a49f4288f", + "reference": "9298a2d490e064f0c72d86ecc9f4792a49f4288f", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Appwrite\\ClamAV\\": "src/ClamAV" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Eldad Fux", + "email": "eldad@appwrite.io" + } + ], + "description": "ClamAV network and pipe client for PHP", + "keywords": [ + "anti virus", + "appwrite", + "clamav", + "php" + ], + "time": "2019-05-11 15:12:28" + }, { "name": "bacon/bacon-qr-code", "version": "2.0.0", diff --git a/docker-compose.yml b/docker-compose.yml index 2217f44a1..3e2400c66 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -44,7 +44,7 @@ services: restart: unless-stopped clamav: - image: registry.gitlab.com/appwrite/appwrite/clamav:v1.0.0 + image: appwrite/clamav:v1.0.2 restart: unless-stopped influxdb: diff --git a/docker-stack.yml b/docker-stack.yml index 84d494260..3a121a387 100644 --- a/docker-stack.yml +++ b/docker-stack.yml @@ -97,7 +97,7 @@ services: - redis clamav: - image: registry.gitlab.com/appwrite/appwrite/clamav:v1.0.0 + image: appwrite/clamav:v1.0.2 restart: unless-stopped networks: - clamav diff --git a/src/ClamAV/ClamAV.php b/src/ClamAV/ClamAV.php deleted file mode 100644 index 1a192cc77..000000000 --- a/src/ClamAV/ClamAV.php +++ /dev/null @@ -1,112 +0,0 @@ -getSocket(); - - socket_send($socket, $command, strlen($command), 0); - socket_recv($socket, $return, self::CLAMAV_MAXP, 0); - socket_close($socket); - - return $return; - } - - /* `ping` command is used to see whether Clamd is alive or not */ - public function ping() - { - $return = $this->sendCommand('PING'); - return strcmp($return, 'PONG') ? true : false; - } - - /* `version` is used to receive the version of Clamd */ - public function version() - { - return trim($this->sendCommand('VERSION')); - } - - /* `reload` Reload Clamd */ - public function reload() - { - return $this->sendCommand('RELOAD'); - } - - /* `shutdown` Shutdown Clamd */ - public function shutdown() - { - return $this->sendCommand('SHUTDOWN'); - } - - /* `fileScan` is used to scan single file. */ - public function fileScan($file) - { - $out = $this->sendCommand('SCAN ' . $file); - - list($file, $stats) = explode(':', $out); - - $result = trim($stats); - - return ($result === 'OK'); - } - - /* `continueScan` is used to scan multiple files/directories. */ - public function continueScan($file) - { - $return = array(); - - foreach(explode("\n", trim($this->sendCommand('CONTSCAN ' . $file))) as $results ) { - list($file, $stats) = explode(':', $results); - array_push($return, array( 'file' => $file, 'stats' => trim($stats) )); - } - - return $return; - } - - /* `streamScan` is used to scan a buffer. */ - public function streamScan($buffer) - { - $port = null; - $socket = null; - $command = 'STREAM'; - $return = null; - - $socket = $this->getSocket(); - - socket_send($socket, $command, strlen($command), 0); - socket_recv($socket, $return, self::CLAMAV_MAXP, 0); - - sscanf($return, 'PORT %d\n', $port); - - $stream = socket_create(AF_INET, SOCK_STREAM, 0); - - socket_connect($stream, self::CLAMAV_MAXP, $port); - socket_send($stream, $buffer, strlen($buffer), 0); - socket_close($stream); - - socket_recv($socket, $return, self::CLAMAV_MAXP, 0); - - socket_close($socket); - - return array('stats' => trim(str_replace('stream: ', '', $return))); - } -} diff --git a/src/ClamAV/Network.php b/src/ClamAV/Network.php deleted file mode 100644 index f341621ba..000000000 --- a/src/ClamAV/Network.php +++ /dev/null @@ -1,48 +0,0 @@ -host = $host; - $this->port = $port; - } - - /** - * @return resource - * @throws \Exception - */ - protected function getSocket() - { - $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'); - } - return $socket; - } -} \ No newline at end of file diff --git a/src/ClamAV/Pipe.php b/src/ClamAV/Pipe.php deleted file mode 100644 index ee4a9775f..000000000 --- a/src/ClamAV/Pipe.php +++ /dev/null @@ -1,30 +0,0 @@ -pip = $pip; - } - - protected function getSocket() - { - $socket = socket_create(AF_UNIX, SOCK_STREAM, 0); - socket_connect($socket, $this->pip); - return $socket; - } -} \ No newline at end of file