1 | <?php |
||
16 | class Item implements iScannable{ |
||
17 | /** |
||
18 | * Scanned fileid (optional) |
||
19 | * @var int |
||
20 | */ |
||
21 | protected $id; |
||
22 | |||
23 | /** |
||
24 | * File view |
||
25 | * @var \OC\Files\View |
||
26 | */ |
||
27 | protected $view; |
||
28 | |||
29 | /** |
||
30 | * Path relative to the view |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $path; |
||
34 | |||
35 | /** |
||
36 | * file handle, user to read from the file |
||
37 | * @var resource |
||
38 | */ |
||
39 | protected $fileHandle; |
||
40 | |||
41 | /** |
||
42 | * Portion size |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $chunkSize; |
||
46 | |||
47 | /** |
||
48 | * Is filesize match the size conditions |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $isValidSize; |
||
52 | |||
53 | /** |
||
54 | * @var IL10N |
||
55 | */ |
||
56 | private $l10n; |
||
57 | 4 | ||
58 | 4 | public function __construct(IL10N $l10n, $view, $path, $id = null) { |
|
59 | $this->l10n = $l10n; |
||
60 | 4 | ||
61 | if (!is_object($view)){ |
||
62 | $this->logError('Can\'t init filesystem view.', $id, $path); |
||
63 | throw new \RuntimeException(); |
||
64 | } |
||
65 | 4 | ||
66 | 1 | if(!$view->file_exists($path)) { |
|
67 | 1 | $this->logError('File does not exist.', $id, $path); |
|
68 | throw new \RuntimeException(); |
||
69 | } |
||
70 | 4 | ||
71 | 1 | if (is_null($id)){ |
|
72 | 1 | $this->id = $view->getFileInfo($path)->getId(); |
|
73 | 3 | } else { |
|
74 | $this->id = $id; |
||
75 | } |
||
76 | 4 | ||
77 | 4 | $this->view = $view; |
|
78 | $this->path = $path; |
||
79 | 4 | ||
80 | $this->isValidSize = $view->filesize($path) > 0; |
||
81 | 4 | ||
82 | 4 | $application = new \OCA\Files_Antivirus\AppInfo\Application(); |
|
83 | 4 | $config = $application->getContainer()->query('AppConfig'); |
|
84 | 4 | $this->chunkSize = $config->getAvChunkSize(); |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Is this file good for scanning? |
||
89 | * @return boolean |
||
90 | 3 | */ |
|
91 | 3 | public function isValid() { |
|
92 | 3 | $isValid = !$this->view->is_dir($this->path) && $this->isValidSize; |
|
93 | return $isValid; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Reads a file portion by portion until the very end |
||
98 | * @return string|boolean |
||
99 | 3 | */ |
|
100 | 3 | public function fread() { |
|
114 | |||
115 | /** |
||
116 | * Action to take if this item is infected |
||
117 | * @param Status $status |
||
118 | * @param boolean $isBackground |
||
119 | 1 | */ |
|
120 | public function processInfected(Status $status, $isBackground) { |
||
121 | $application = new \OCA\Files_Antivirus\AppInfo\Application(); |
||
122 | $appConfig = $application->getContainer()->query('AppConfig'); |
||
123 | $infectedAction = $appConfig->getAvInfectedAction(); |
||
124 | |||
125 | $shouldDelete = !$isBackground || ($isBackground && $infectedAction === 'delete'); |
||
126 | |||
127 | $message = $shouldDelete ? Activity::MESSAGE_FILE_DELETED : ''; |
||
128 | |||
129 | \OC::$server->getActivityManager()->publishActivity( |
||
130 | 'files_antivirus', |
||
131 | Activity::SUBJECT_VIRUS_DETECTED, |
||
132 | array($this->path, $status->getDetails()), |
||
133 | $message, |
||
134 | array(), |
||
135 | $this->path, |
||
136 | '', |
||
137 | $this->view->getOwner($this->path), |
||
138 | Activity::TYPE_VIRUS_DETECTED, |
||
139 | Activity::PRIORITY_HIGH |
||
140 | 1 | ); |
|
141 | if ($isBackground) { |
||
142 | if ($shouldDelete) { |
||
143 | $this->logError('Infected file deleted. ' . $status->getDetails()); |
||
144 | $this->deleteFile(); |
||
145 | } else { |
||
146 | $this->logError('File is infected. ' . $status->getDetails()); |
||
147 | } |
||
148 | } else { |
||
149 | $this->logError('Virus(es) found: ' . $status->getDetails()); |
||
150 | //remove file |
||
151 | $this->deleteFile(); |
||
152 | Notification::sendMail($this->path); |
||
153 | $message = $this->l10n->t( |
||
154 | "Virus detected! Can't upload the file %s", |
||
155 | array(basename($this->path)) |
||
156 | ); |
||
157 | \OCP\JSON::error(array("data" => array( "message" => $message))); |
||
158 | exit(); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Action to take if this item status is unclear |
||
164 | * @param Status $status |
||
165 | * @param boolean $isBackground |
||
166 | */ |
||
167 | public function processUnchecked(Status $status, $isBackground) { |
||
|
|||
168 | //TODO: Show warning to the user: The file can not be checked |
||
169 | $this->logError('Not Checked. ' . $status->getDetails()); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Action to take if this item status is not infected |
||
174 | * @param Status $status |
||
175 | * @param boolean $isBackground |
||
176 | */ |
||
177 | public function processClean(Status $status, $isBackground) { |
||
178 | if (!$isBackground) { |
||
179 | return; |
||
180 | } |
||
181 | try { |
||
182 | $stmt = \OCP\DB::prepare('DELETE FROM `*PREFIX*files_antivirus` WHERE `fileid` = ?'); |
||
183 | $result = $stmt->execute(array($this->id)); |
||
184 | if (\OCP\DB::isError($result)) { |
||
185 | //TODO: Use logger |
||
186 | $this->logError(__METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage($result)); |
||
187 | } |
||
188 | $stmt = \OCP\DB::prepare('INSERT INTO `*PREFIX*files_antivirus` (`fileid`, `check_time`) VALUES (?, ?)'); |
||
189 | $result = $stmt->execute(array($this->id, time())); |
||
190 | if (\OCP\DB::isError($result)) { |
||
191 | $this->logError(__METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage($result)); |
||
192 | } |
||
193 | } catch(\Exception $e) { |
||
194 | \OCP\Util::writeLog('files_antivirus', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Check if the end of file is reached |
||
200 | * @return boolean |
||
201 | 3 | */ |
|
202 | 3 | private function feof() { |
|
211 | |||
212 | /** |
||
213 | * Opens a file for reading |
||
214 | * @throws \RuntimeException |
||
215 | 3 | */ |
|
216 | 3 | private function getFileHandle() { |
|
226 | |||
227 | /** |
||
228 | * Delete infected file |
||
229 | 3 | */ |
|
230 | 3 | private function deleteFile() { |
|
240 | |||
241 | 1 | /** |
|
242 | 1 | * @param string $message |
|
243 | 1 | */ |
|
244 | 1 | public function logDebug($message) { |
|
250 | 1 | ||
251 | 1 | /** |
|
252 | * @param string $message |
||
253 | * @param int $id optional |
||
254 | * @param string $path optional |
||
255 | */ |
||
256 | public function logError($message, $id=null, $path=null) { |
||
267 | } |
||
268 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.