Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LocalClam.php « Scanner « lib - github.com/nextcloud/files_antivirus.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d314a91d51ec3100e61ba854885ae90a493036d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
 * Copyright (c) 2014 Victor Dubiniuk <victor.dubiniuk@gmail.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */


namespace OCA\Files_Antivirus\Scanner;

use OCA\Files_Antivirus\AppConfig;
use OCA\Files_Antivirus\StatusFactory;
use OCP\ILogger;

class LocalClam extends ScannerBase {

	/**
	 * @var string
	 */
	protected $avPath;

	/**
	 * STDIN and STDOUT descriptors
	 * @var array of resources
	 */
	private $pipes = [];

	/**
	 * Process handle
	 * @var resource
	 */
	private $process;

	public function __construct(AppConfig $config, ILogger $logger, StatusFactory $statusFactory) {
		parent::__construct($config, $logger, $statusFactory);

		// get the path to the executable
		$this->avPath = escapeshellcmd($this->appConfig->getAvPath());

		// check that the executable is available
		if (!file_exists($this->avPath)) {
			throw new \RuntimeException('The antivirus executable could not be found at ' . $this->avPath);
		}
	}
	
	/**
	 * @return void
	 */
	public function initScanner() {
		parent::initScanner();
		
		// using 2>&1 to grab the full command-line output.
		$cmd = $this->avPath . ' ' . $this->appConfig->getCmdline() . ' - 2>&1';
		$descriptorSpec = [
			0 => ['pipe', 'r'], // STDIN
			1 => ['pipe', 'w']  // STDOUT
		];
		
		$this->process = proc_open($cmd, $descriptorSpec, $this->pipes);
		if (!is_resource($this->process)) {
			throw new \RuntimeException('Error starting process');
		}
		$this->writeHandle = $this->pipes[0];
	}
	
	/**
	 * @return void
	 */
	protected function shutdownScanner() {
		@fclose($this->pipes[0]);
		$output = stream_get_contents($this->pipes[1]);
		@fclose($this->pipes[1]);
		
		$result = proc_close($this->process);
		$this->logger->debug(
			'Exit code :: ' . $result . ' Response :: ' . $output,
			['app' => 'files_antivirus']
		);
		$this->status->parseResponse($output, $result);
	}
}