1 | <?php |
||
20 | class AvirWrapper extends Wrapper{ |
||
21 | |||
22 | /** |
||
23 | * Modes that are used for writing |
||
24 | * @var array |
||
25 | */ |
||
26 | private $writingModes = array('r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'); |
||
27 | |||
28 | /** |
||
29 | * @var AppConfig |
||
30 | */ |
||
31 | protected $appConfig; |
||
32 | |||
33 | /** |
||
34 | * @var \OCA\Files_Antivirus\ScannerFactory |
||
35 | */ |
||
36 | protected $scannerFactory; |
||
37 | |||
38 | /** |
||
39 | * @var IL10N |
||
40 | */ |
||
41 | protected $l10n; |
||
42 | |||
43 | /** |
||
44 | * @var ILogger; |
||
45 | */ |
||
46 | protected $logger; |
||
47 | |||
48 | /** |
||
49 | * @param array $parameters |
||
50 | */ |
||
51 | 2 | public function __construct($parameters) { |
|
58 | |||
59 | /** |
||
60 | * Asynchronously scan data that are written to the file |
||
61 | * @param string $path |
||
62 | * @param string $mode |
||
63 | * @return resource | bool |
||
64 | */ |
||
65 | 3 | public function fopen($path, $mode){ |
|
66 | 3 | $stream = $this->storage->fopen($path, $mode); |
|
67 | 3 | if (is_resource($stream) |
|
68 | 3 | && $this->isWritingMode($mode) |
|
69 | 3 | && $this->isScannableSize(basename($path)) |
|
70 | ) { |
||
71 | try { |
||
72 | 2 | $scanner = $this->scannerFactory->getScanner(); |
|
73 | 2 | $scanner->initScanner(); |
|
74 | 2 | return CallBackWrapper::wrap( |
|
75 | 2 | $stream, |
|
76 | 2 | null, |
|
77 | function ($data) use ($scanner){ |
||
78 | 2 | $scanner->onAsyncData($data); |
|
79 | 2 | }, |
|
80 | 2 | function () use ($scanner, $path) { |
|
81 | 2 | $status = $scanner->completeAsyncScan(); |
|
82 | 2 | if (intval($status->getNumericStatus()) === \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED){ |
|
83 | //prevent from going to trashbin |
||
84 | 2 | if (App::isEnabled('files_trashbin')) { |
|
85 | 2 | \OCA\Files_Trashbin\Storage::preRenameHook([ |
|
86 | 2 | Filesystem::signal_param_oldpath => '', |
|
87 | Filesystem::signal_param_newpath => '' |
||
88 | ]); |
||
89 | } |
||
90 | |||
91 | 2 | $owner = $this->getOwner($path); |
|
92 | 2 | $this->unlink($path); |
|
93 | |||
94 | 2 | if (App::isEnabled('files_trashbin')) { |
|
95 | 2 | \OCA\Files_Trashbin\Storage::postRenameHook([]); |
|
96 | } |
||
97 | 2 | $this->logger->warning( |
|
98 | 2 | 'Infected file deleted. ' . $status->getDetails() |
|
99 | 2 | . ' Account: ' . $owner . ' Path: ' . $path, |
|
100 | 2 | ['app' => 'files_antivirus'] |
|
101 | ); |
||
102 | |||
103 | 2 | \OC::$server->getActivityManager()->publishActivity( |
|
104 | 2 | 'files_antivirus', |
|
105 | 2 | Activity::SUBJECT_VIRUS_DETECTED, |
|
106 | 2 | [$path, $status->getDetails()], |
|
107 | 2 | Activity::MESSAGE_FILE_DELETED, |
|
108 | 2 | [], |
|
109 | 2 | $path, |
|
110 | 2 | '', |
|
111 | 2 | $owner, |
|
112 | 2 | Activity::TYPE_VIRUS_DETECTED, |
|
113 | 2 | Activity::PRIORITY_HIGH |
|
114 | ); |
||
115 | |||
116 | 2 | throw new InvalidContentException( |
|
117 | 2 | $this->l10n->t( |
|
118 | 2 | 'Virus %s is detected in the file. Upload cannot be completed.', |
|
119 | 2 | $status->getDetails() |
|
120 | ) |
||
121 | ); |
||
122 | } |
||
123 | 2 | } |
|
124 | ); |
||
125 | 2 | } catch (\Exception $e){ |
|
126 | 2 | $message = implode(' ', [ __CLASS__, __METHOD__, $e->getMessage()]); |
|
127 | 2 | $this->logger->warning($message); |
|
128 | } |
||
129 | } |
||
130 | 3 | return $stream; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Checks whether passed mode is suitable for writing |
||
135 | * @param string $mode |
||
136 | * @return bool |
||
137 | */ |
||
138 | 3 | private function isWritingMode($mode){ |
|
139 | // Strip unessential binary/text flags |
||
140 | 3 | $cleanMode = str_replace( |
|
141 | 3 | ['t', 'b'], |
|
142 | 3 | ['', ''], |
|
143 | 3 | $mode |
|
144 | ); |
||
145 | 3 | return in_array($cleanMode, $this->writingModes); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * checks the size for webdav PUT requests. defaults to true |
||
150 | * @param $filename |
||
151 | * @return bool |
||
152 | */ |
||
153 | 2 | private function isScannableSize($filename) { |
|
164 | } |
||
165 |