1 | <?php |
||
22 | class AvirWrapper extends Wrapper{ |
||
23 | |||
24 | /** |
||
25 | * Modes that are used for writing |
||
26 | * @var array |
||
27 | */ |
||
28 | private $writingModes = ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+']; |
||
29 | |||
30 | /** @var ScannerFactory */ |
||
31 | protected $scannerFactory; |
||
32 | |||
33 | /** @var IL10N */ |
||
34 | protected $l10n; |
||
35 | |||
36 | /** @var ILogger */ |
||
37 | protected $logger; |
||
38 | |||
39 | /** @var ActivityManager */ |
||
40 | protected $activityManager; |
||
41 | |||
42 | /** |
||
43 | * @param array $parameters |
||
44 | */ |
||
45 | 2 | public function __construct($parameters) { |
|
52 | |||
53 | /** |
||
54 | * Asynchronously scan data that are written to the file |
||
55 | * @param string $path |
||
56 | * @param string $mode |
||
57 | * @return resource | bool |
||
58 | */ |
||
59 | 1 | public function fopen($path, $mode){ |
|
60 | 1 | $stream = $this->storage->fopen($path, $mode); |
|
61 | 1 | if (is_resource($stream) && $this->isWritingMode($mode)) { |
|
62 | try { |
||
63 | 1 | $scanner = $this->scannerFactory->getScanner(); |
|
64 | $scanner->initScanner(); |
||
65 | return CallbackWrapper::wrap( |
||
66 | $stream, |
||
67 | null, |
||
68 | function ($data) use ($scanner){ |
||
69 | $scanner->onAsyncData($data); |
||
70 | }, |
||
71 | function () use ($scanner, $path) { |
||
72 | $status = $scanner->completeAsyncScan(); |
||
73 | if ((int)$status->getNumericStatus() === Status::SCANRESULT_INFECTED){ |
||
74 | //prevent from going to trashbin |
||
75 | if (App::isEnabled('files_trashbin')) { |
||
76 | \OCA\Files_Trashbin\Storage::preRenameHook([ |
||
77 | 'oldpath' => '', |
||
78 | 'newpath' => '' |
||
79 | ]); |
||
80 | } |
||
81 | |||
82 | $owner = $this->getOwner($path); |
||
83 | $this->unlink($path); |
||
84 | |||
85 | if (App::isEnabled('files_trashbin')) { |
||
86 | \OCA\Files_Trashbin\Storage::preRenameHook([ |
||
87 | 'oldpath' => '', |
||
88 | 'newpath' => '' |
||
89 | ]); |
||
90 | } |
||
91 | $this->logger->warning( |
||
92 | 'Infected file deleted. ' . $status->getDetails() |
||
93 | . ' Account: ' . $owner . ' Path: ' . $path, |
||
94 | ['app' => 'files_antivirus'] |
||
95 | ); |
||
96 | |||
97 | $activity = $this->activityManager->generateEvent(); |
||
98 | $activity->setApp(Application::APP_NAME) |
||
99 | ->setSubject(Provider::SUBJECT_VIRUS_DETECTED_UPLOAD, [$status->getDetails()]) |
||
100 | ->setMessage(Provider::MESSAGE_FILE_DELETED) |
||
101 | ->setObject('', 0, $path) |
||
102 | ->setAffectedUser($owner) |
||
103 | ->setType(Provider::TYPE_VIRUS_DETECTED); |
||
104 | $this->activityManager->publish($activity); |
||
105 | |||
106 | $this->logger->error('Infected file deleted. ' . $status->getDetails() . |
||
107 | ' File: ' . $path . ' Acccount: ' . $owner, ['app' => 'files_antivirus']); |
||
108 | |||
109 | throw new InvalidContentException( |
||
110 | $this->l10n->t( |
||
111 | 'Virus %s is detected in the file. Upload cannot be completed.', |
||
112 | $status->getDetails() |
||
113 | ) |
||
114 | ); |
||
115 | } |
||
116 | } |
||
117 | ); |
||
118 | 1 | } catch (\Exception $e){ |
|
119 | 1 | $this->logger->logException($e); |
|
120 | } |
||
121 | } |
||
122 | 1 | return $stream; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Checks whether passed mode is suitable for writing |
||
127 | * @param string $mode |
||
128 | * @return bool |
||
129 | */ |
||
130 | 1 | private function isWritingMode($mode){ |
|
139 | } |
||
140 |