Completed
Push — master ( 621618...6f1917 )
by Roeland
10s
created

Item::fread()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 8.8571
cc 5
eloc 8
nc 5
nop 0
crap 30
1
<?php
2
/**
3
 * Copyright (c) 2015 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
namespace OCA\Files_Antivirus;
10
11
use OCA\Files_Antivirus\Activity\Provider;
12
use OCA\Files_Antivirus\AppInfo\Application;
13
use OCA\Files_Antivirus\Db\ItemMapper;
14
use OCP\Activity\IManager as ActivityManager;
15
use OCP\App;
16
use OCP\AppFramework\Db\DoesNotExistException;
17
use OCP\Files\File;
18
use OCP\IL10N;
19
use OCP\ILogger;
20
21
class Item {
22
	/**
23
	 * file handle, user to read from the file
24
	 * @var resource
25
	 */
26
	protected $fileHandle;
27
28
	/** @var AppConfig */
29
	private $config;
30
31
	/** @var ActivityManager */
32
	private $activityManager;
33
34
	/** @var ItemMapper */
35
	private $itemMapper;
36
37
	/** @var ILogger */
38
	private $logger;
39
40
	/** @var File */
41
	private $file;
42
43
	/**
44
	 * Item constructor.
45
	 *
46
	 * @param AppConfig $appConfig
47
	 * @param ActivityManager $activityManager
48
	 * @param ItemMapper $itemMapper
49
	 * @param ILogger $logger
50
	 * @param File $file
51
	 */
52
	public function __construct(AppConfig $appConfig,
53
								ActivityManager $activityManager,
54
								ItemMapper $itemMapper,
55
								ILogger $logger,
56
								File $file) {
57
		$this->config = $appConfig;
58
		$this->activityManager = $activityManager;
59
		$this->itemMapper = $itemMapper;
60
		$this->logger = $logger;
61
		$this->file = $file;
62
	}
63
64
	/**
65
	 * Reads a file portion by portion until the very end
66
	 * @return string|boolean
67
	 */
68
	public function fread() {
69
		if (!($this->file->getSize() > 0)) {
70
			return false;
71
		}
72
73
		if (is_null($this->fileHandle)) {
74
			$this->getFileHandle();
75
		}
76
77
		if (!is_null($this->fileHandle) && !$this->feof()) {
78
			return fread($this->fileHandle, $this->config->getAvChunkSize());
79
		}
80
		return false;
81
	}
82
83
	/**
84
	 * Action to take if this item is infected
85
	 */
86
	public function processInfected(Status $status) {
87
		$infectedAction = $this->config->getAvInfectedAction();
88
		
89
		$shouldDelete = $infectedAction === 'delete';
90
		
91
		$message = $shouldDelete ? Provider::MESSAGE_FILE_DELETED : '';
92
93
		$activity = $this->activityManager->generateEvent();
94
		$activity->setApp(Application::APP_NAME)
95
			->setSubject(Provider::SUBJECT_VIRUS_DETECTED, [$this->file->getPath(), $status->getDetails()])
96
			->setMessage($message)
97
			->setObject('file', $this->file->getId(), $this->file->getPath())
98
			->setAffectedUser($this->file->getOwner()->getUID())
99
			->setType(Provider::TYPE_VIRUS_DETECTED);
100
		$this->activityManager->publish($activity);
101
102
		if ($shouldDelete) {
103
			$this->logError('Infected file deleted. ' . $status->getDetails());
104
			$this->deleteFile();
105
		} else {
106
			$this->logError('File is infected. '  . $status->getDetails());
107
		}
108
	}
109
110
	/**
111
	 * Action to take if this item status is unclear
112
	 * @param Status $status
113
	 */
114
	public function processUnchecked(Status $status) {
115
		//TODO: Show warning to the user: The file can not be checked
116
		$this->logError('Not Checked. ' . $status->getDetails());
117
	}
118
119
	/**
120
	 * Action to take if this item status is not infected
121
	 */
122
	public function processClean() {
123
		try {
124
			try {
125
				$item = $this->itemMapper->findByFileId($this->file->getId());
126
				$this->itemMapper->delete($item);
127
			} catch (DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
128
				//Just ignore
129
			}
130
131
			$item = new \OCA\Files_Antivirus\Db\Item();
132
			$item->setFileid($this->file->getId());
133
			$item->setCheckTime(time());
134
			$this->itemMapper->insert($item);
135
		} catch(\Exception $e) {
136
			$this->logger->error(__METHOD__.', exception: '.$e->getMessage(), ['app' => 'files_antivirus']);
137
		}
138
	}
139
140
	/**
141
	 * Check if the end of file is reached
142
	 * @return boolean
143
	 */
144
	private function feof() {
145
		$isDone = feof($this->fileHandle);
146
		if ($isDone) {
147
			$this->logDebug('Scan is done');
148
			fclose($this->fileHandle);
149
			$this->fileHandle = null;
150
		}
151
		return $isDone;
152
	}
153
154
	/**
155
	 * Opens a file for reading
156
	 * @throws \RuntimeException
157
	 */
158
	private function getFileHandle() {
159
		$fileHandle = $this->file->fopen('r');
160
		if ($fileHandle === false) {
161
			$this->logError('Can not open for reading.');
162
			throw new \RuntimeException();
163
		}
164
165
		$this->logDebug('Scan started');
166
		$this->fileHandle = $fileHandle;
167
	}
168
169
	/**
170
	 * Delete infected file
171
	 */
172
	private function deleteFile() {
173
		//prevent from going to trashbin
174
		if (App::isEnabled('files_trashbin')) {
175
			\OCA\Files_Trashbin\Storage::preRenameHook([]);
176
		}
177
		$this->file->delete();
178
		if (App::isEnabled('files_trashbin')) {
179
			\OCA\Files_Trashbin\Storage::postRenameHook([]);
180
		}
181
	}
182
183
	/**
184
	 * @param string $message
185
	 */
186
	public function logDebug($message) {
187
		$extra = ' File: ' . $this->file->getId()
188
				. 'Account: ' . $this->file->getOwner()->getUID()
189
				. ' Path: ' . $this->file->getPath();
190
		$this->logger->debug($message . $extra, ['app' => 'files_antivirus']);
191
	}
192
193
	/**
194
	 * @param string $message
195
	 */
196
	public function logError($message) {
197
		$ownerInfo = 'Account: ' . $this->file->getOwner()->getUID();
198
		$extra = ' File: ' . $this->file->getId()
199
				. $ownerInfo 
200
				. ' Path: ' . $this->file->getPath();
201
		$this->logger->error($message . $extra, ['app' => 'files_antivirus']);
202
	}
203
}
204