Completed
Push — antivirus-update ( fcda38...b6d85d )
by Victor
08:14
created

External::initScanner()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
c 1
b 0
f 0
nc 10
nop 0
dl 0
loc 21
rs 8.7624
1
<?php
2
/**
3
 * Copyright (c) 2014 Victor Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
10
namespace OCA\Files_Antivirus\Scanner;
11
12
class External extends \OCA\Files_Antivirus\Scanner {
13
	
14
	// Daemon/socket mode
15
	private $useSocket;
16
	
17
	/**
18
	 * Handle to write data into
19
	 * @var resource 
20
	 */
21
	private $writeHandle;
22
	
23
	public function __construct($config){
24
		$this->appConfig = $config;
25
		$this->useSocket = $this->appConfig->getAvMode() === 'socket';
26
	}
27
	
28
	protected function initScanner(){
29
		parent::initScanner();
30
		
31
		if ($this->useSocket){
32
			$avSocket = $this->appConfig->getAvSocket();
33
			$this->writeHandle = stream_socket_client('unix://' . $avSocket, $errno, $errstr, 5);
34
			if (!$this->writeHandle) {
35
				throw new \RuntimeException('Cannot connect to "' . $avSocket . '": ' . $errstr . ' (code ' . $errno . ')');
36
			}
37
		} else {
38
			$avHost = $this->appConfig->getAvHost();
39
			$avPort = $this->appConfig->getAvPort();
40
			$this->writeHandle = ($avHost && $avPort) ? @fsockopen($avHost, $avPort) : false;
41
			if (!$this->writeHandle) {
42
				throw new \RuntimeException('The clamav module is not configured for daemon mode.');
43
			}
44
		}
45
46
		// request scan from the daemon
47
		fwrite($this->writeHandle, "nINSTREAM\n");
48
	}
49
	
50
	protected function shutdownScanner(){
51
		fwrite($this->writeHandle, pack('N', 0));
52
		$response = fgets($this->writeHandle);
53
		\OCP\Util::writeLog('files_antivirus', 'Response :: '.$response, \OCP\Util::DEBUG);
54
		fclose($this->writeHandle);
55
		
56
		$this->status->parseResponse($response);
57
	}
58
	
59
	protected function getWriteHandle(){
60
		return $this->writeHandle;
61
	}
62
	
63
	protected function prepareChunk($data){
64
		$chunk_len = pack('N', strlen($data));
65
		return $chunk_len.$data;
66
	}
67
}
68