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

scanner.php « lib - github.com/nextcloud/files_antivirus.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 69842491b570470eb3b5abec294a5d072a41db03 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php

/**
* ownCloud - files_antivirus
*
* @author Manuel Deglado
* @copyright 2012 Manuel Deglado manuel.delgado@ucr.ac.cr
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Files_Antivirus;

abstract class Scanner {
	
	/**
	 * Scan result
	 * @var Status
	 */
	protected $status;

	/**
	 * If scanning was done part by part
	 * the first detected infected part is stored here
	 * @var Status
	 */
	protected $infectedStatus;

	/** @var  int */
	protected $byteCount;

	/** @var  resource */
	protected $writeHandle;

	/** @var \OCA\Files_Antivirus\AppConfig */
	protected $appConfig;

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

	/** @var bool */
	protected $isLogUsed = false;

	/** @var bool */
	protected $isAborted = false;

	/**
	 * Close used resources
	 */
	abstract protected function shutdownScanner();


	public function getStatus(){
		if ($this->infectedStatus instanceof Status){
			return $this->infectedStatus;
		}
		if ($this->status instanceof Status){
			return $this->status;
		}
		return new Status();
	}

	/**
	 * Synchronous scan
	 * @param IScannable $item
	 * @return Status
	 */
	public function scan(IScannable $item) {
		$this->initScanner();

		while (false !== ($chunk = $item->fread())) {
			$this->writeChunk($chunk);
		}
		
		$this->shutdownScanner();
		return $this->getStatus();
	}
	
	/**
	 * Async scan - new portion of data is available
	 * @param string $data
	 */
	public function onAsyncData($data){
		$this->writeChunk($data);
	}
	
	/**
	 * Async scan - resource is closed
	 * @return Status
	 */
	public function completeAsyncScan(){
		$this->shutdownScanner();
		return $this->getStatus();
	}
	
	/**
	 * Open write handle. etc
	 */
	public function initScanner(){
		$this->byteCount = 0;
		if ($this->status instanceof Status && $this->status->getNumericStatus() === Status::SCANRESULT_INFECTED){
			$this->infectedStatus = clone $this->status;
		}
		$this->status = new Status();
	}

	/**
	 * @param string $chunk
	 */
	protected function writeChunk($chunk){
		$this->fwrite(
			$this->prepareChunk($chunk)
		);
	}

	/**
	 * @param string $data
	 */
	protected final function fwrite($data){
		if ($this->isAborted){
			return;
		}

		$dataLength = strlen($data);
		$streamSizeLimit = intval($this->appConfig->getAvStreamMaxLength());
		if ($this->byteCount + $dataLength > $streamSizeLimit){
			\OC::$server->getLogger()->debug(
				'reinit scanner',
				['app' => 'files_antivirus']
			);
			$this->shutdownScanner();
			$isReopenSuccessful = $this->retry();
		} else {
			$isReopenSuccessful = true;
		}

		if (!$isReopenSuccessful || !$this->writeRaw($data)){
			if (!$this->isLogUsed) {
				$this->isLogUsed = true;
				\OC::$server->getLogger()->warning(
					'Failed to write a chunk. Check if Stream Length matches StreamMaxLength in ClamAV daemon settings',
					['app' => 'files_antivirus']
				);
			}
			// retry on error
			$isRetrySuccessful = $this->retry() && $this->writeRaw($data);
			$this->isAborted = !$isRetrySuccessful;
		}
	}

	/**
	 * @return bool
	 */
	protected function retry(){
		$this->initScanner();
		if (!is_null($this->lastChunk)) {
			return $this->writeRaw($this->lastChunk);
		}
		return true;
	}

	/**
	 * @param $data
	 * @return bool
	 */
	protected function writeRaw($data){
		$dataLength = strlen($data);
		$bytesWritten = @fwrite($this->getWriteHandle(), $data);
		if ($bytesWritten === $dataLength){
			$this->byteCount += $bytesWritten;
			$this->lastChunk = $data;
			return true;
		}
		return false;
	}

	/**
	 * Get a resource to write data into
	 * @return resource
	 */
	protected function getWriteHandle(){
		return $this->writeHandle;
	}

	/**
	 * Prepare chunk (if required)
	 * @param string $data
	 * @return string
	 */
	protected function prepareChunk($data){
		return $data;
	}
}